Similar Problems

Similar Problems not available

Active Businesses - Leetcode Solution

Companies:

LeetCode:  Active Businesses Leetcode Solution

Difficulty: Medium

Topics: database  

The Active Businesses problem on Leetcode is a problem where we are given a list of business IDs and their respective daily revenue. We are also given a K value which represents the total number of continuous days in which a business has to earn revenue greater than or equal to a given threshold value T for that business to be considered active.

Our task is to find the IDs of all the active businesses in the given list.

To solve this problem, we can follow the following approach:

Step 1: Initialize a dictionary to store the daily revenue of each business ID. We can use a loop to iterate through the input list and populate this dictionary.

Step 2: Initialize a list to store the IDs of all active businesses.

Step 3: Iterate through the dictionary and for each business ID, check if it is active or not. To check this, we need to iterate through the daily revenue of that business from day 1 to day N (where N is the total number of days for which we have daily revenue data). For each window of K continuous days, we can calculate the average revenue. If this average revenue is greater than or equal to the threshold value T, then the business is considered active and we add its ID to the list of active businesses.

Step 4: Return the list of active businesses.

Here is the Python code to implement this approach:

def activeBusinesses(businesses, k, t):
    # Initialize a dictionary to store the daily revenue of each business ID
    revenue = {}
    for b, r in businesses:
        if b in revenue:
            revenue[b].append(r)
        else:
            revenue[b] = [r]

    # Initialize a list to store the IDs of all active businesses
    active = []

    # Iterate through the dictionary and check for each business ID if it's active or not
    for b in revenue:
        for i in range(len(revenue[b]) - k + 1):
            if sum(revenue[b][i:i+k]) / k >= t:
                active.append(b)
                break

    return active

This code has a time complexity of O(N*K), where N is the total number of business IDs and K is the size of the window we use to check if a business is active or not. Depending on the size of the input data, we may need to optimize this algorithm further. However, for most practical purposes, this approach should work fine.

Active Businesses Solution Code

1