You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:
You should build the array arr which has the following properties:
arr has exactly n integers.1 <= arr[i] <= m where (0 <= i < n).arr, the value search_cost is equal to k.Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7.
Example 1:
Input: n = 2, m = 3, k = 1 Output: 6 Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
Example 2:
Input: n = 5, m = 2, k = 3 Output: 0 Explanation: There are no possible arrays that satisfy the mentioned conditions.
Example 3:
Input: n = 9, m = 1, k = 1 Output: 1 Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
Constraints:
1 <= n <= 501 <= m <= 1000 <= k <= nWe can use dynamic programming to solve this problem by maintaining a 3D array where dp[i][j][k] represents the number of ways to construct an array of length i, with values up to j and exactly k comparisons.
Recurrence relations can be defined by considering whether you want to extend the array by a value that does or doesn't increase the number of comparisons. Use modulo operations to manage large numbers.
The initial step is to set the dp array for arrays of length 1 with exactly one comparison. It can also reach up to any value from 1 to m. As you build arrays, you either keep the maximum unchanged or increase it with a new maximum. The complexity arises from trying to keep a 3D table of results for all possible combinations of parameters and then aggregating those results.
C++
Time Complexity: O(n * m^2 * k). Space Complexity: O(n * m * k).
This approach uses recursion with memoization to avoid recomputing the solutions for subproblems. The recursive function attempts to build the array incrementally by deciding, step by step, which value to add, and whether it will increase the maximum so far. By caching previously computed results, it reduces repeated calculations.
The Java solution initializes a 3D memoization table filled with -1 (uncomputed states) and recursively defines valid states for the dynamic structure of the array. It traverses through not only possible values to append but also considers whether adding the value modifies the comparison metric used (k comparisons).
JavaScript
Time Complexity: O(n * m * k). Space Complexity: O(n * m * k) due to recursion stack and memoization storage.
| Approach | Complexity |
|---|---|
| Dynamic Programming with 3D Table | Time Complexity: O(n * m^2 * k). Space Complexity: O(n * m * k). |
| Recursion with Memoization | Time Complexity: O(n * m * k). Space Complexity: O(n * m * k) due to recursion stack and memoization storage. |
Build Array Where You Can Find The Maximum Exactly K Comparisons |DP Concepts & Qns-15|Leetcode-1420 • codestorywithMIK • 12,156 views views
Watch 9 more video solutions →Practice Build Array Where You Can Find The Maximum Exactly K Comparisons with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor