-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
39 lines (31 loc) · 915 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int dp[605][105][105];
int f(int idx, vector<pair<int, int>>& arr, int m, int n) {
// base
if (idx == arr.size())
return 0;
// dp check
if (dp[idx][m][n] != -1)
return dp[idx][m][n];
// main
int cur0 = arr[idx].first;
int cur1 = arr[idx].second;
// exclude this idx
if (m - cur0 < 0 or n - cur1 < 0)
return f(idx + 1, arr, m, n);
// satisfy + include/exclude this idx
int incl = 1 + f(idx + 1, arr, m - cur0, n - cur1);
int excl = f(idx + 1, arr, m, n);
return dp[idx][m][n] = max(incl, excl);
}
int findMaxForm(vector<string>& strs, int m, int n) {
int k = strs.size();
vector<pair<int, int>> arr(k);
for (int i = 0; i < k; ++i) {
arr[i] = {count(strs[i].begin(), strs[i].end(), '0'), count(strs[i].begin(), strs[i].end(), '1')};
}
memset(dp, -1, sizeof(dp));
return f(0, arr, m, n);
}
};