Skip to content

Commit eb7905b

Browse files
committed
一刷474
1 parent 1202d7d commit eb7905b

File tree

5 files changed

+105
-35
lines changed

5 files changed

+105
-35
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,14 +3343,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
33433343
//|{doc_base_url}/0473-matchsticks-to-square.adoc[题解]
33443344
//|Medium
33453345
//|
3346-
//
3347-
//|{counter:codes}
3348-
//|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
3349-
//|{source_base_url}/_0474_OnesAndZeroes.java[Java]
3350-
//|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
3351-
//|Medium
3352-
//|
3353-
//
3346+
3347+
|{counter:codes}
3348+
|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
3349+
|{source_base_url}/_0474_OnesAndZeroes.java[Java]
3350+
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
3351+
|Medium
3352+
|
3353+
33543354
//|{counter:codes}
33553355
//|{leetcode_base_url}/heaters/[475. Heaters^]
33563356
//|{source_base_url}/_0475_Heaters.java[Java]

docs/0474-ones-and-zeroes.adoc

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,77 @@
11
[#0474-ones-and-zeroes]
2-
= 474. Ones and Zeroes
2+
= 474. 一和零
33

4-
{leetcode}/problems/ones-and-zeroes/[LeetCode - Ones and Zeroes^]
4+
https://leetcode.cn/problems/ones-and-zeroes/[LeetCode - 474. 一和零 ^]
55

6-
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
6+
给你一个二进制字符串数组 `+strs+` 和两个整数 `+m+``+n+`
77

8-
For now, suppose you are a dominator of *m* `0s` and *n* `1s` respectively. On the other hand, there is an array with strings consisting of only `0s` and `1s`.
8+
请你找出并返回 `+strs+` 的最大子集的长度,该子集中 *最多*`+m+``+0+``+n+``+1+`
99

10-
Now your task is to find the maximum number of strings that you can form with given *m* `0s` and *n* `1s`. Each `0` and `1` can be used at most *once*.
10+
如果 `+x+` 的所有元素也是 `+y+` 的元素,集合 `+x+` 是集合 `+y+`*子集*
1111

12-
*Note:*
12+
*示例 1:*
1313

14+
....
15+
输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
16+
输出:4
17+
解释:最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。
18+
其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
19+
....
1420

15-
. The given numbers of `0s` and `1s` will both not exceed `100`
16-
. The size of given string array won't exceed `600`.
21+
*示例 2:*
1722

23+
....
24+
输入:strs = ["10", "0", "1"], m = 1, n = 1
25+
输出:2
26+
解释:最大的子集是 {"0", "1"} ,所以答案是 2 。
27+
....
1828

19-
2029

21-
*Example 1:*
30+
*提示:*
2231

23-
[subs="verbatim,quotes,macros"]
24-
----
25-
*Input:* Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
26-
*Output:* 4
27-
28-
*Explanation:* This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
29-
----
32+
* `+1 <= strs.length <= 600+`
33+
* `+1 <= strs[i].length <= 100+`
34+
* `+strs[i]+` 仅由 `+'0'+``+'1'+` 组成
35+
* `+1 <= m, n <= 100+`
3036
31-
3237
33-
*Example 2:*
3438
35-
[subs="verbatim,quotes,macros"]
36-
----
37-
*Input:* Array = {"10", "0", "1"}, m = 1, n = 1
38-
*Output:* 2
39+
== 思路分析
3940

40-
*Explanation:* You could form "10", but then you'd have nothing left. Better form "0" and "1".
41-
----
41+
思路:把总共的 0 和 1 的个数视为背包的容量,每一个字符串视为装进背包的物品。这道题就可以使用 0-1 背包问题的思路完成,这里的目标值是能放进背包的字符串的数量。
4242

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

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

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

4749
[[src-0474]]
50+
[tabs]
51+
====
52+
一刷::
53+
+
54+
--
4855
[{java_src_attr}]
4956
----
5057
include::{sourcedir}/_0474_OnesAndZeroes.java[tag=answer]
5158
----
59+
--
60+
61+
// 二刷::
62+
// +
63+
// --
64+
// [{java_src_attr}]
65+
// ----
66+
// include::{sourcedir}/_0474_OnesAndZeroes_2.java[tag=answer]
67+
// ----
68+
// --
69+
====
70+
71+
72+
== 参考资料
73+
74+
. https://leetcode.cn/problems/ones-and-zeroes/solutions/814806/yi-he-ling-by-leetcode-solution-u2z2/[474. 一和零 - 官方题解^]
75+
76+
5277

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ include::0462-minimum-moves-to-equal-array-elements-ii.adoc[leveloffset=+1]
10221022

10231023
// include::0473-matchsticks-to-square.adoc[leveloffset=+1]
10241024

1025-
// include::0474-ones-and-zeroes.adoc[leveloffset=+1]
1025+
include::0474-ones-and-zeroes.adoc[leveloffset=+1]
10261026

10271027
// include::0475-heaters.adoc[leveloffset=+1]
10281028

logbook/202406.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@
929929
|{doc_base_url}/0031-next-permutation.adoc[题解]
930930
|❌ 一脸懵逼,可以看成寻找下一个更大的数。这样更容易理解。
931931
932+
|{counter:codes}
933+
|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
934+
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
935+
|❌ 动态规划,0-1 背包问题。
936+
937+
932938
|===
933939
934940
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0474_OnesAndZeroes {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2024-10-22 19:47:11
9+
*/
10+
public int findMaxForm(String[] strs, int m, int n) {
11+
int length = strs.length;
12+
int[][] bits = new int[2][length];
13+
for (int i = 0; i < length; i++) {
14+
for (int j = 0; j < strs[i].length(); j++) {
15+
char c = strs[i].charAt(j);
16+
bits[c - '0'][i]++;
17+
}
18+
}
19+
// 确定 dp 数组(dp table)以及下标的含义
20+
// dp 数组如何初始化
21+
int[][][] dp = new int[length + 1][m + 1][n + 1];
22+
// 确定遍历顺序
23+
for (int i = 1; i <= length; i++) {
24+
int zero = bits[0][i - 1];
25+
int one = bits[1][i - 1];
26+
for (int j = 0; j <= m; j++) {
27+
for (int k = 0; k <= n; k++) {
28+
// 确定递推公式
29+
dp[i][j][k] = dp[i - 1][j][k];
30+
if (j >= zero && k >= one) {
31+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1);
32+
}
33+
}
34+
}
35+
}
36+
return dp[length][m][n];
37+
}
38+
// end::answer[]
39+
}

0 commit comments

Comments
 (0)