I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, prefix sums.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/discuss/586576/C%2B%2B-Bottom-Up-Dynamic-Programming-with-Explanation
- time: O(NKM*M), space:O(NMK)
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numOfArrays.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(NKM*M), space:O(NMK)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 160 ms, faster than 45.75% of C++ online submissions for Build Array Where You Can Find The Maximum Exactly K Comparisons.
02//Memory Usage: 10.2 MB, less than 100.00% of C++ online submissions for Build Array Where You Can Find The Maximum Exactly K Comparisons.
03//https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/discuss/586576/C%2B%2B-Bottom-Up-Dynamic-Programming-with-Explanation
04//time: O(NKM*M), space:O(NMK)
05class Solution {
06public:
07 int numOfArrays(int n, int m, int k) {
08 //+1 for padding because we want to use 1-based index
09 vector<vector<vector<int>>> dp(n+1, vector(m+1, vector(k+1, 0)));
10 int MOD = 1e9+7;
11
12 /*
13 base case
14 length 1, max element ?, cost 1
15 */
16 for(int j = 1; j <= m; j++){
17 //we have only one way to construct a length 1 array
18 dp[1][j][1] = 1;
19 }
20
21 for(int a = 1; a <= n; a++){
22 for(int b = 1; b <= m; b++){
23 for(int c = 1; c <= k; c++){
24 //base case
25 // if(a == 1 && c == 1) continue;
26
27 /*
28 case 1: search cost not increase
29 we have an array of length a-1, max element b, and cost c,
30 if we append an element in [1, b], the length will increase by 1,
31 and the cost will not change
32 */
33 //the *b means we can append "b" kinds of element
34 int S = 0;
35 //to avoid overflow, equivalent to S += dp[a-1][b][c]*b % MOD;
36 for(int x = 1; x <= b; x++){
37 S = (S + dp[a-1][b][c]) % MOD;
38 }
39
40 /*
41 case 2: search cost increase
42 we have an array of length a-1, max element [1,b-1], and cost c-1,
43 if we add the element "b", the length will increase by 1,
44 and the cost will also increase by 1
45 */
46 for(int x = 1; x < b; x++){
47 S = (S + dp[a-1][x][c-1]) % MOD;
48 }
49
50 // dp[a][b][c] = S % MOD;
51 dp[a][b][c] = (dp[a][b][c] + S) % MOD;
52 }
53
54 }
55 }
56
57 int ans = 0;
58
59 for(int b = 1; b <= m; b++){
60 ans = (ans + dp[n][b][k]) % MOD;
61 }
62
63 return ans;
64 }
65};
66
67//O(NKM) solution
68//Runtime: 28 ms, faster than 87.67% of C++ online submissions for Build Array Where You Can Find The Maximum Exactly K Comparisons.
69//Memory Usage: 13.8 MB, less than 100.00% of C++ online submissions for Build Array Where You Can Find The Maximum Exactly K Comparisons.
70//https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/discuss/586576/C++-Bottom-Up-Dynamic-Programming-with-Explanation/509746
71//time: O(NKM), space:O(NMK)
72class Solution {
73public:
74 int numOfArrays(int n, int m, int k) {
75 //+1 for padding because we want to use 1-based index
76 vector<vector<vector<int>>> dp(n+1, vector(m+1, vector(k+1, 0)));
77 /*
78 prefix sum,
79 pre[a][b][c] corresponds to the sum of ways when max element is in the range [1,b]
80 meaning of a, c are still the same
81 */
82 vector<vector<vector<int>>> pre(n+1, vector(m+1, vector(k+1, 0)));
83 int MOD = 1e9+7;
84
85 /*
86 base case
87 length 1, max element ?, cost 1
88 */
89 for(int j = 1; j <= m; j++){
90 //we have only one way to construct a length 1 array
91 dp[1][j][1] = 1;
92 //ways of just smaller element plus current array
93 pre[1][j][1] = pre[1][j-1][1] + 1;
94 }
95
96 for(int a = 1; a <= n; a++){
97 for(int b = 1; b <= m; b++){
98 for(int c = 1; c <= k; c++){
99 //base case
100 if(a == 1 && c == 1) continue;
101
102 /*
103 case 1: search cost not increase
104 we have an array of length a-1, max element b, and cost c,
105 if we append an element in [1, b], the length will increase by 1,
106 and the cost will not change
107 */
108 //the *b means we can append "b" kinds of element
109 long long S = 0;
110 S += (1LL*dp[a-1][b][c]*b) % MOD;
111
112 /*
113 case 2: search cost increase
114 we have an array of length a-1, max element [1,b-1], and cost c-1,
115 if we add the element "b", the length will increase by 1,
116 and the cost will also increase by 1
117 */
118 //prefix sum, max element range from 1 to b-1
119 S = (S + pre[a-1][b-1][c-1]) % MOD;
120
121 //this should be used when base case is skipped!
122 dp[a][b][c] = S % MOD;
123 //safe when base case is not skipped
124 // dp[a][b][c] = (dp[a][b][c] + S) % MOD;
125 // prefix sum whose max element is b-1, plus current number of ways
126 pre[a][b][c] = (pre[a][b-1][c] + dp[a][b][c]) % MOD;
127 }
128
129 }
130 }
131
132 int ans = 0;
133
134 for(int b = 1; b <= m; b++){
135 ans = (ans + dp[n][b][k]) % MOD;
136 }
137
138 return ans;
139 }
140};
Cost