-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathones-and-zeroes.cpp
29 lines (28 loc) · 1.03 KB
/
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
class Solution {
public:
//就是将二维的01背包问题转化成三维01背包问题,没有什么新意。最需要注意的就是数组索引了,其他感觉都问题不大。
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<vector<int>>> tmp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1)));
for(int i=1;i<=strs.size();i++){
for(int j=0;j<=m;j++){
for(int h=0;h<=n;h++){
auto pp=helper(strs[i-1]);
int x=pp.first,y=pp.second;
tmp[i][j][h]=tmp[i-1][j][h];
if(j>=x && h>=y)
tmp[i][j][h]=max(tmp[i][j][h],tmp[i-1][j-x][h-y]+1);
//cout<<tmp[i][j][h]<<endl;
}
}
}
return tmp[strs.size()][m][n];
}
pair<int,int> helper(string s){
int one=0,zero=0;
for(const auto &c:s){
if(c=='0') zero++;
else one++;
}
return make_pair(zero,one);
}
};