Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

2474. Customers With Strictly Increasing Purchases

Database

Explanation

Given a list of customer orders, we need to find the number of customers who have strictly increasing purchases compared to the previous order. We can solve this problem by iterating through the customer orders and keeping track of the longest increasing subsequence ending at each order.

Here's the step-by-step algorithm:

  1. Initialize an array dp of size equal to the number of customer orders, filled with 1s to represent the minimum subsequence length ending at each order.
  2. Iterate through the customer orders starting from the second order.
  3. For each order, compare it with all previous orders to find the longest increasing subsequence length ending at the current order.
  4. Update the dp array with the maximum subsequence length found so far.
  5. Return the sum of the dp array to get the total number of customers with strictly increasing purchases.

Time complexity: O(n^2) where n is the number of customer orders Space complexity: O(n) for the dp array

class Solution {
    public int maxProfit(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        int maxProfit = 1;
        
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            maxProfit = Math.max(maxProfit, dp[i]);
        }
        
        return maxProfit;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.