Similar Problems

Similar Problems not available

Customers With Strictly Increasing Purchases - Leetcode Solution

Companies:

LeetCode:  Customers With Strictly Increasing Purchases Leetcode Solution

Difficulty: Hard

Topics: database  

The Customers With Strictly Increasing Purchases problem on Leetcode asks us to find the number of customers who have strictly increasing purchases in consecutive months.

To solve this problem, we can iterate through the input array and keep track of the previous purchase amount for each customer. If the current purchase amount is strictly greater than the previous purchase amount, we can increment a counter. At the end, we return the counter.

Here’s the code to solve the Customers With Strictly Increasing Purchases problem:

def increasing_customers(purchases):
    count = 0
    prev_purchases = {}
    for i in range(len(purchases)):
        customer_id = purchases[i][0]
        purchase_amount = purchases[i][1]
        if customer_id not in prev_purchases:
            prev_purchases[customer_id] = purchase_amount
        else:
            if purchase_amount > prev_purchases[customer_id]:
                count += 1
            prev_purchases[customer_id] = purchase_amount
    return count

The function takes an input array purchases where each element is a tuple (customer_id, purchase_amount) representing a purchase by a customer. The function returns the number of customers with strictly increasing purchases.

We start by initializing a counter count to 0 and a dictionary prev_purchases to keep track of the previous purchase amount for each customer. We iterate through the input array using a for loop and extract the customer ID and purchase amount for the current purchase. If the customer ID is not in the dictionary, we add it to the dictionary and set its corresponding previous purchase amount to the current purchase amount. If the customer ID is in the dictionary, we check if the current purchase amount is strictly greater than the previous purchase amount. If it is, we increment the counter count. Finally, we update the dictionary with the current purchase amount for the customer ID.

At the end of the loop, we return the counter count representing the number of customers with strictly increasing purchases.

Overall, the time complexity of this solution is O(n) where n is the length of the input array since we iterate through the array once. The space complexity is also O(n) because we use a dictionary to store the previous purchase amount for each customer.

Customers With Strictly Increasing Purchases Solution Code

1