Skip to content

Commit 95068d4

Browse files
committed
三刷474
1 parent 1904fea commit 95068d4

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

docs/0474-ones-and-zeroes.adoc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ https://leetcode.cn/problems/ones-and-zeroes/[LeetCode - 474. 一和零 ^]
4141

4242
物品一个一个尝试,容量一点一点尝试,每个物品分类讨论的标准是:选与不选。
4343

44-
dp[i−1][j][k] // 不选择当前考虑的字符串,至少是这个数值
44+
`dp[i−1][j][k]` // 不选择当前考虑的字符串,至少是这个数值
4545

46-
dp[i−1][j−当前字符串使用0的个数][k−当前字符串使用1的个数] + 1 // 选择当前考虑的字符串
46+
`dp[i−1][j−当前字符串使用0的个数][k−当前字符串使用1的个数] + 1` // 选择当前考虑的字符串
4747

4848
[[src-0474]]
4949
[tabs]
@@ -65,11 +65,24 @@ include::{sourcedir}/_0474_OnesAndZeroes.java[tag=answer]
6565
include::{sourcedir}/_0474_OnesAndZeroes_2.java[tag=answer]
6666
----
6767
--
68+
69+
三刷::
70+
+
71+
--
72+
[{java_src_attr}]
73+
----
74+
include::{sourcedir}/_0474_OnesAndZeroes_3.java[tag=answer]
75+
----
76+
--
6877
====
6978

7079

7180
== 参考资料
7281

82+
. https://leetcode.cn/problems/ones-and-zeroes/solutions/3038333/yi-bu-bu-si-kao-cong-ji-yi-hua-sou-suo-d-lqio/[474. 一和零 - 一步步思考:从记忆化搜索到递推到空间优化!^]
83+
. https://leetcode.cn/problems/ones-and-zeroes/solutions/72372/dong-tai-gui-hua-zhuan-huan-wei-0-1-bei-bao-wen-ti/[474. 一和零 - 动态规划(转换为 0-1 背包问题)^]
84+
. https://leetcode.cn/problems/ones-and-zeroes/solutions/814942/gong-shui-san-xie-xiang-jie-ru-he-zhuan-174wv/[474. 一和零 - 详解如何转换「背包问题」,以及逐步空间优化^]
85+
. https://leetcode.cn/problems/ones-and-zeroes/solutions/567850/474-yi-he-ling-01bei-bao-xiang-jie-by-ca-s9vr/[474. 一和零 - 带你学透01背包(附背包详细攻略)^]
7386
. https://leetcode.cn/problems/ones-and-zeroes/solutions/814806/yi-he-ling-by-leetcode-solution-u2z2/[474. 一和零 - 官方题解^]
7487

7588

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ endif::[]
6363
|{counter:codes2503e}
6464
|{leetcode_base_url}/ones-and-zeroes/[474. 一和零^]
6565
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
66-
|❌
66+
|❌ ⭕️
6767

6868
|{counter:codes2503e}
6969
|{leetcode_base_url}/coin-change/[322. 零钱兑换]
@@ -2640,6 +2640,11 @@ endif::[]
26402640
|{doc_base_url}/0053-maximum-subarray.adoc[题解]
26412641
|✅ 动态规划。stem:[f(x)] 表示从 `0` 到第 `x` 个元素的最大子数组和,如果第 `x-1` 个元素的最大子数组之和大于 `0`,则可以继续向上“盖楼”;当小于等于 `0` 时,可以抛弃前面的元素从新开始。则递推公式是: stem:[f(x)=max(f(x-1),0)+a[x\]]。这就是 Kadane 算法。
26422642

2643+
|{counter:codes2503e}
2644+
|{leetcode_base_url}/ones-and-zeroes/[474. 一和零^]
2645+
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
2646+
|⭕️ 动态规划。能识别出来 0/1 背包问题。但是有多维限制添加就懵逼了!
2647+
26432648
|===
26442649

26452650

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0474_OnesAndZeroes_3 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-12-16 22:41:12
9+
*/
10+
public int findMaxForm(String[] strs, int m, int n) {
11+
int[][] bits = new int[strs.length][2];
12+
for (int i = 0; i < strs.length; i++) {
13+
String str = strs[i];
14+
for (char c : str.toCharArray()) {
15+
bits[i][c - '0']++;
16+
}
17+
}
18+
int[][][] dp = new int[strs.length + 1][m + 1][n + 1];
19+
for (int x = 1; x <= strs.length; x++) {
20+
int zero = bits[x - 1][0];
21+
int one = bits[x - 1][1];
22+
for (int y = 0; y <= m; y++) {
23+
for (int z = 0; z <= n; z++) {
24+
if (y >= zero && z >= one) {
25+
dp[x][y][z] = Math.max(dp[x - 1][y][z], dp[x - 1][y - zero][z - one] + 1);
26+
} else {
27+
dp[x][y][z] = dp[x - 1][y][z];
28+
}
29+
}
30+
}
31+
}
32+
return dp[strs.length][m][n];
33+
}
34+
// end::answer[]
35+
}

0 commit comments

Comments
 (0)