-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathOnes and Zeroes.cpp
43 lines (28 loc) · 1.07 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Solution by Rahul Surana
***********************************************************
You are given an array of binary strings strs and two integers m and n.
Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset.
A set x is a subset of a set y if all elements of x are also elements of y.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m+1,vector<int>(n+1,0));
for(int i = 0; i < strs.size(); i++){
int z = 0,o = 0;
for(int j = 0; j < strs[i].size(); j++){
if(strs[i][j] == '0') z++;
else o++;
}
for(int j = m; j >= z; j--){
for(int k = n; k >= o; k--){
dp[j][k] = max(dp[j][k], dp[j-z][k-o]+1);
}
}
}
return dp[m][n];
}
};