Ones and Zeroes
For now, suppose you are a dominator ofm0sandn1srespectively. On the other hand, there is an array with strings consisting of only0sand1s.
Now your task is to find the maximum number of strings that you can form with given m0sand n1s. Each0and1can be used at most once.
public class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for(String str: strs) {
int ones = 0, zeros = 0;
for(char c: str.toCharArray()) {
if(c == '0')
zeros ++;
else
ones ++;
}
for(int i = m; i >= zeros; i --) {
for(int j = n; j >= ones; j --) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
}
}