Similar Problems

Similar Problems not available

Restaurant Growth - Leetcode Solution

Companies:

LeetCode:  Restaurant Growth Leetcode Solution

Difficulty: Medium

Topics: database  

Restaurant Growth is a problem on LeetCode that asks us to find the number of restaurants that have a higher revenue in each year than the year before, given an array of revenues for each restaurant in each year.

Solution:

Here we need to find the number of restaurants that have a higher revenue in each year than the year before. For this we need to loop through each year and then loop through each restaurant’s revenue in that year. If the restaurant’s revenue in the current year is greater than the restaurant’s revenue in the previous year, then we increment a counter for that year.

The code for the solution would be as follows:

def restaurantGrowth(revenues):
    count = 0
    for i in range(1, len(revenues)):
        year_count = 0
        for j in range(len(revenues[i])):
            if revenues[i][j] > revenues[i-1][j]:
                year_count += 1
        if year_count == len(revenues[i]):
            count += 1
    return count

In the above solution, we loop through the revenues array starting from the second year (i.e., index 1). For each year, we initialize a counter for that year (year_count) to 0. We then loop through the revenues for each restaurant in that year, and if the revenue for that restaurant in the current year is greater than the revenue in the previous year, then we increment the year_count.

At the end of each year, we check if all the restaurants had a higher revenue in the current year than the previous year (i.e., the year_count is equal to the length of the revenue array for that year). If that is the case, then we increment the count variable by 1.

Finally, we return the count variable which represents the number of restaurants that had a higher revenue in each year than the year before.

Overall, the time complexity of this solution is O(n*m), where n is the number of years and m is the number of restaurants in each year. However, if we assume that the number of restaurants and the number of years are the same (i.e., n=m=k), then the time complexity can be reduced to O(k^2).

Restaurant Growth Solution Code

1