-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution474.java
More file actions
33 lines (31 loc) · 851 Bytes
/
Solution474.java
File metadata and controls
33 lines (31 loc) · 851 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2018/08/23
*/
public class Solution474 {
public int findMaxForm(String[] strs, int m, int n) {
if (strs == null || strs.length == 0) {
return 0;
}
int[][] dp = new int[m + 1][n + 1];
// 每个字符串只能用一次
for (String s : strs) {
int zeros = 0, ones = 0;
for (char c : s.toCharArray()) {
if ('0' == c) {
++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];
}
}