Leetcode-editorials
Contribute
  • Leetcode question of the day
  • 08-August
    • 26. Maximum Length of Pair Chain
    • 28. Implement stack using queues
    • 29. Minimum Penalty for a Shop
    • 30. Minimum Replacements to Sort the Array
    • 31. Minimum Number of Taps to Open to Water a Garden
  • 09-September
    • 01. Counting Bits
    • 02. Extra Characters in a String
    • 03. Unique Paths
    • 04. Linked List Cycle
    • 05. Copy List with Random Pointer
    • 06. Split Linked List in Parts
    • 07. Reverse Linked List II
    • 08. Pascal's Triangle
    • 09. Combination Sum IV
    • 10. Count All Valid Pickup and Delivery Options
    • 11. Group the People Given the Group Size They Belong To
    • 12. Minimum Deletions to Make Character Frequencies Unique
    • 13. Candy
    • 14. Reconstruct Itinerary
    • 15. Min Cost to Connect All Points
    • 16. Path With Minimum Effort
    • 17. Shortest Path Visiting All Nodes
    • 18. The K Weakest Rows in a Matrix
    • 19. Find the Duplicate Number
    • 20. Minimum Operations to Reduce X to Zero
    • 21-Median of Two Sorted Arrays
    • 22- Is Subsequence
    • 23- Longest String Chain
    • 24- Champagne Tower
    • 25- Find the Difference
    • 26- Remove Duplicate Letters
    • 27- Decoded String at Index
    • 28- Sort Array By Parity
    • 29- Monotonic Array
    • 30- 132 Pattern
  • 10-October
    • 01. Reverse Words in a String III
    • 02. Remove Colored Pieces if Both Neighbors are the Same Color
    • 03. Number of Good Pairs
    • 04. Design HashMap
    • 05. Majority Element II
    • 06. Integer Break
    • 07. Build Array Where You Can Find The Maximum Exactly K Comparisons
  • 11-November
    • 01. Find Mode in Binary Search Tree
    • 02. Count Nodes Equal to Average of Subtree
    • 03. Build an Array With Stack Operations
    • 04. Last Moment Before All Ants Fall Out of a Plank
    • 07. Eliminate Maximum Number of Monsters
  • Leetcode Contests
    • Weekly Contest
      • Weekly-Contest-360
Powered by GitBook
On this page
  • My Approach
  • Time and Auxiliary Space Complexity
  • Code (C++)
  • Contribution and Support

Was this helpful?

Edit on GitHub
  1. 10-October

07. Build Array Where You Can Find The Maximum Exactly K Comparisons

Previous06. Integer BreakNext11-November

Last updated 1 year ago

Was this helpful?

The problem can be found at the following link:

My Approach

  1. Define a private function called "solve" with the following parameters:

    • n: The remaining number of elements to be placed in the array.

    • m: The maximum value that can be used to fill an element in the array.

    • k: The remaining allowed changes to the previously placed value.

    • prev: The previously placed value in the array.

    • dp: A three-dimensional vector used for memoization.

  2. Check if n is 0 and k is 0, indicating that we have successfully constructed the array. Return 1 in this case.

  3. Check if k is less than 0 or n is less than 0, indicating an invalid state. Return 0 in these cases.

  4. Check if the result for the current state (n, k, prev) is already calculated and stored in the memoization table dp. If yes, return the stored result.

  5. Initialize a variable ans to 0 to keep track of the number of valid arrays.

  6. Iterate through values from 1 to m (inclusive) representing the next element to be placed in the array.

  7. If prev is less than the current value i, recursively call the "solve" function with reduced n, k, and prev updated to i. Add the result to ans.

  8. If prev is greater than or equal to the current value i, recursively call the "solve" function with reduced n and k, keeping prev unchanged. Add the result to ans.

  9. Calculate the final result as ans % mod, where mod is defined as 1e9+7.

  10. Store the calculated result in the memoization table dp for the current state (n, k, prev).

  11. In the public function "numOfArrays," check if k is greater than n. If yes, return 0 because it's not possible to construct the array with more allowed changes than the remaining elements.

  12. Initialize a three-dimensional vector dp of size (n+1) x (k+1) x (m+1) with all values set to -1 for memoization.

Time and Auxiliary Space Complexity

  • Time Complexity: O(n)

  • Auxiliary Space Complexity: O(n)

Code (C++)


class Solution {
private:
    int solve(int n, int m, int k, int prev, vector<vector<vector<int>>> &dp){
        if(n == 0 && k == 0){
            return 1;
        }
        if(k < 0 || n < 0)
            return 0;
        if(dp[n][k][prev] != -1)
            return dp[n][k][prev];

        int ans = 0;
        for(int i = 1; i <= m; i++){
            if(prev < i)
                ans = (ans + solve(n - 1, m, k - 1, i, dp)) % mod;
            else
                ans = (ans + solve(n - 1, m, k, prev, dp)) % mod;
        }
        return dp[n][k][prev] = ans % mod;
    }
public:
    long long mod = 1e9+7;
    int numOfArrays(int n, int m, int k) {
        if(k > n)
            return 0;
        vector<vector<vector<int>>> dp(n + 1, vector<vector<int>> (k + 1, vector<int> (m + 1, -1)));
        return solve(n, m, k, 0, dp);
    }
};

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.

Question Link
discussion section
rishabhv12/Daily-Leetcode-Solution