|
| 1 | +## 07. Build Array Where You Can Find The Maximum Exactly K Comparisons |
| 2 | + |
| 3 | + |
| 4 | +The problem can be found at the following link: [Question Link](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/) |
| 5 | + |
| 6 | + |
| 7 | +### My Approach |
| 8 | + |
| 9 | + |
| 10 | +1. Define a private function called "solve" with the following parameters: |
| 11 | + - `n`: The remaining number of elements to be placed in the array. |
| 12 | + - `m`: The maximum value that can be used to fill an element in the array. |
| 13 | + - `k`: The remaining allowed changes to the previously placed value. |
| 14 | + - `prev`: The previously placed value in the array. |
| 15 | + - `dp`: A three-dimensional vector used for memoization. |
| 16 | + |
| 17 | +2. Check if `n` is 0 and `k` is 0, indicating that we have successfully constructed the array. Return 1 in this case. |
| 18 | + |
| 19 | +4. Check if `k` is less than 0 or `n` is less than 0, indicating an invalid state. Return 0 in these cases. |
| 20 | + |
| 21 | +5. 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. |
| 22 | + |
| 23 | +6. Initialize a variable `ans` to 0 to keep track of the number of valid arrays. |
| 24 | + |
| 25 | +7. Iterate through values from 1 to `m` (inclusive) representing the next element to be placed in the array. |
| 26 | + |
| 27 | +8. 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`. |
| 28 | + |
| 29 | +9. 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`. |
| 30 | + |
| 31 | +10. Calculate the final result as `ans % mod`, where `mod` is defined as 1e9+7. |
| 32 | + |
| 33 | +11. Store the calculated result in the memoization table `dp` for the current state (n, k, prev). |
| 34 | + |
| 35 | +12. 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. |
| 36 | + |
| 37 | +13. Initialize a three-dimensional vector `dp` of size (n+1) x (k+1) x (m+1) with all values set to -1 for memoization. |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +### Time and Auxiliary Space Complexity |
| 42 | + |
| 43 | +- **Time Complexity**: `O(n)` |
| 44 | +- **Auxiliary Space Complexity**: `O(n)` |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +### Code (C++) |
| 49 | + |
| 50 | +```cpp |
| 51 | + |
| 52 | +class Solution { |
| 53 | +private: |
| 54 | + int solve(int n, int m, int k, int prev, vector<vector<vector<int>>> &dp){ |
| 55 | + if(n == 0 && k == 0){ |
| 56 | + return 1; |
| 57 | + } |
| 58 | + if(k < 0 || n < 0) |
| 59 | + return 0; |
| 60 | + if(dp[n][k][prev] != -1) |
| 61 | + return dp[n][k][prev]; |
| 62 | + |
| 63 | + int ans = 0; |
| 64 | + for(int i = 1; i <= m; i++){ |
| 65 | + if(prev < i) |
| 66 | + ans = (ans + solve(n - 1, m, k - 1, i, dp)) % mod; |
| 67 | + else |
| 68 | + ans = (ans + solve(n - 1, m, k, prev, dp)) % mod; |
| 69 | + } |
| 70 | + return dp[n][k][prev] = ans % mod; |
| 71 | + } |
| 72 | +public: |
| 73 | + long long mod = 1e9+7; |
| 74 | + int numOfArrays(int n, int m, int k) { |
| 75 | + if(k > n) |
| 76 | + return 0; |
| 77 | + vector<vector<vector<int>>> dp(n + 1, vector<vector<int>> (k + 1, vector<int> (m + 1, -1))); |
| 78 | + return solve(n, m, k, 0, dp); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +``` |
| 83 | +
|
| 84 | +### Contribution and Support |
| 85 | +
|
| 86 | +For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://leetcode.com/discuss/general-discussion). We welcome your input and aim to foster a collaborative learning environment. |
| 87 | +
|
| 88 | +If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [rishabhv12/Daily-Leetcode-Solution](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository. |
0 commit comments