https://leetcode.com/problems/ones-and-zeroes/
DP问题的递归方式一定要有两种思想,一种是从前向后递归,另一种是从后向前递归。具体表现形式就是递归遍历dp起始位置是0还是len - 1。
本题之中是对当前遍历到的str更新所有i >= str.m & j >= str.n位置的dp。
public class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
int res = 0;
for (String str : strs) {
int[] arr = count(str);
for (int i = m; i >= arr[0]; i--) {
for (int j = n; j >= arr[1]; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - arr[0]][j - arr[1]] + 1);
}
}
}
return dp[m][n];
}
private int[] count(String str) {
int[] res = new int[2];
for (int i = 0; i < str.length(); i++) {
res[str.charAt(i) - '0']++;
}
return res;
}
}