This problem looks busy at first, but the accepted solution is built around one steady invariant. For 474. Ones and Zeroes, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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.
The notes already sitting in the source point us in the right direction:
- bounded backpack problem with 2-dimensional constraint
- time: O(#strs * m * n), space: O(#strs * m * n)
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 findMaxForm.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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(#strs * m * n), space: O(m * n)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//bounded backpack problem with 2-dimensional constraint
02//Runtime: 380 ms, faster than 32.03% of C++ online submissions for Ones and Zeroes.
03//Memory Usage: 101.9 MB, less than 14.72% of C++ online submissions for Ones and Zeroes.
04//time: O(#strs * m * n), space: O(#strs * m * n)
05class Solution {
06public:
07 int findMaxForm(vector<string>& strs, int m, int n) {
08 vector<vector<vector<int>>> dp(strs.size(), vector<vector<int>>(m+1, vector(n+1, 0)));
09
10 for(int i = 0; i < strs.size(); i++){
11 //the cost of char '0'
12 int cm = count(strs[i].begin(), strs[i].end(), '0');
13 //the cost of char '1'
14 int cn = count(strs[i].begin(), strs[i].end(), '1');
15 for(int j = 0; j <= m; j++){
16 for(int k = 0; k <= n; k++){
17 if(i == 0){
18 dp[i][j][k] = (j >= cm && k >= cn);
19 }else{
20 dp[i][j][k] = max(dp[i-1][j][k], (i > 0 && j >= cm && k >= cn) ? 1 + dp[i-1][j-cm][k-cn] : 0);
21 }
22 // cout << i << ", " << j << ", " << k << " : " << dp[i][j][k] << endl;
23 }
24 }
25 }
26
27 return dp[strs.size()-1][m][n];
28 }
29};
30
31//O(m * n) space
32//Runtime: 300 ms, faster than 44.95% of C++ online submissions for Ones and Zeroes.
33//Memory Usage: 11.5 MB, less than 51.84% of C++ online submissions for Ones and Zeroes.
34//time: O(#strs * m * n), space: O(m * n)
35class Solution {
36public:
37 int findMaxForm(vector<string>& strs, int m, int n) {
38 //bounded backpack problem with 2-dimensional constraint
39 vector<vector<vector<int>>> dp(2, vector<vector<int>>(m+1, vector(n+1, 0)));
40
41 for(int i = 0; i < strs.size(); i++){
42 //the cost of char '0'
43 int cm = count(strs[i].begin(), strs[i].end(), '0');
44 //the cost of char '1'
45 int cn = strs[i].size() - cm;
46 for(int j = 0; j <= m; j++){
47 for(int k = 0; k <= n; k++){
48 if(i == 0){
49 dp[i%2][j][k] = (j >= cm && k >= cn);
50 }else{
51 dp[i%2][j][k] = max(dp[(i-1)%2][j][k], (j >= cm && k >= cn) ? 1 + dp[(i-1)%2][j-cm][k-cn] : 0);
52 }
53 // cout << i << ", " << j << ", " << k << " : " << dp[i%2][j][k] << endl;
54 }
55 }
56 }
57
58 return dp[(strs.size()-1)%2][m][n];
59 }
60};
Cost