-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapscak474. Ones and Zeroes.cpp
94 lines (58 loc) · 2.09 KB
/
Knapscak474. Ones and Zeroes.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// memoization
class Solution {
public:
static const auto _____ = []()
{
// fast IO code : this I understand
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
vector<int>freq(string &s){
vector<int>v(2,0);
for(auto &it:s) v[it-'0']++;
return v;
}
int solve(vector<string>& s,int zeros,int ones,int idx,vector<vector<vector<int>>>&dp){
if(idx>=s.size() || (zeros+ones==0)) return 0;
if(dp[idx][zeros][ones]!=-1) return dp[idx][zeros][ones];
vector<int>count=freq(s[idx]);
int pick=0,notPick=0;
if(zeros>=count[0] && ones>=count[1]){
pick=1+solve(s,zeros-count[0],ones-count[1],idx+1,dp);
}
notPick=solve(s,zeros,ones,idx+1,dp);
return dp[idx][zeros][ones]=max(pick,notPick);
}
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<vector<int>>>dp(strs.size(),vector<vector<int>>(m+1,vector<int>(n+1,-1)));
return solve(strs,m,n,0,dp);
}
};
// tabular dp
class Solution {
public:
vector<int> count(string s){
vector<int>freq(2,0);
for(auto it:s){
freq[it-'0']++;
}
return freq;
}
int findMaxForm(vector<string>& s, int m, int n) {
int size=s.size();
int dp[m+1][n+1];
memset(dp,0,sizeof(dp));
for(int i=0;i<size;i++){
vector<int>freq=count(s[i]);
for(int j=m;j>=freq[0];j--){
for(int k=n;k>=freq[1];k--){
dp[j][k]=max( dp[j-freq[0]][k-freq[1]]+1,dp[j][k]);
}
}
}
return dp[m][n];
}
};