Maximize the Earning

Napoleon chose a city for Advertising his company's product. There are streets in that city. Each day he travels one street. There are N buildings in a street that are located at points 

1,2,3....N(respectively). Each building has some height(Say h meters). Napoleon stands at the point 0. His height is 0.5m. Now Napoleon starts communicating with the people of each building. He can communicate with people of a particular building only if he can see that building. If he succeeds to communicate with any particular building then his boss gives him Rupees. i.e. if he communicates with K buildings in a day, then he will earn K∗Rupees. Now Napoleon wants to know his maximum earnings for each day.

Note: All the points are on a Straight Line and Napoleon is always standing at point 0.

Example:

Input:  n = 6, r = 3, height = [8, 2, 3, 11, 11, 10]
Output: 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long maximizeEarning(long long nlong long r,
                          long long height[])
{

    long long max1 = height[0];
    long long res = 1;
    for (long long i = 1i < ni++)
    {
        if (height[i] > max1)
            res++;
        max1 = max(max1height[i]);
    }
    return res * r;
}
int main()
{

    long long n = 6r = 3;

    long long height[n] = {823111110};

    cout << maximizeEarning(nrheight<< "\n";

    return 0;
}


No comments:

Post a Comment