474. Ones and Zeroes
T: O(len*m*n)
S: O(mn)
這題就像是有兩個容量的背包, 所以結合 01背包精簡後的寫法, 用 2D 去表示兩個背包
```java
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int[][] dp = new int[m+1][n+1];
for (String s : strs) {
int zeros = 0;
int ones = 0;
for (char c : s.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); // 取這個 str 或不取
}
}
}
return dp[m][n];
}
}
```
Last updated
Was this helpful?