LeetCode 2752: Customers with Maximum Number of Transactions on Consecutive Days Solution

Master LeetCode problem 2752 (Customers with Maximum Number of Transactions on Consecutive Days), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2752. Customers with Maximum Number of Transactions on Consecutive Days

Problem Explanation

Explanation

To solve this problem, we can iterate through the list of transactions and keep track of the consecutive transactions for each customer. We can use a HashMap to store the count of consecutive transactions for each customer. After iterating through all transactions, we can then find the customer(s) with the maximum number of consecutive transactions.

Here are the steps:

  1. Create a HashMap to store the count of consecutive transactions for each customer.
  2. Iterate through the list of transactions and update the count of consecutive transactions for each customer.
  3. Find the customer(s) with the maximum number of consecutive transactions.

Time complexity: O(n) where n is the number of transactions. Space complexity: O(n) for the HashMap to store the count of consecutive transactions for each customer.

Solution Code

import java.util.*;

class Solution {
    public List<Integer> getMaxCustomers(int[] transactions) {
        Map<Integer, Integer> consecutiveTransactions = new HashMap<>();
        int maxConsecutive = 0;
        List<Integer> result = new ArrayList<>();

        int start = 0;
        for (int i = 0; i < transactions.length; i++) {
            int customer = transactions[i];
            consecutiveTransactions.put(customer, consecutiveTransactions.getOrDefault(customer, 0) + 1);
            maxConsecutive = Math.max(maxConsecutive, consecutiveTransactions.get(customer));

            if (i - start + 1 - maxConsecutive > 1) {
                consecutiveTransactions.put(transactions[start], consecutiveTransactions.get(transactions[start]) - 1);
                start++;
            }
        }

        for (Map.Entry<Integer, Integer> entry : consecutiveTransactions.entrySet()) {
            if (entry.getValue() == maxConsecutive) {
                result.add(entry.getKey());
            }
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2752 (Customers with Maximum Number of Transactions on Consecutive Days)?

This page provides optimized solutions for LeetCode problem 2752 (Customers with Maximum Number of Transactions on Consecutive Days) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2752 (Customers with Maximum Number of Transactions on Consecutive Days)?

The time complexity for LeetCode 2752 (Customers with Maximum Number of Transactions on Consecutive Days) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2752 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2752 in Java, C++, or Python.

Back to LeetCode Solutions