Skip to content

Commit

Permalink
一刷474
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Oct 22, 2024
1 parent 1202d7d commit eb7905b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 35 deletions.
16 changes: 8 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3343,14 +3343,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
//|{doc_base_url}/0473-matchsticks-to-square.adoc[题解]
//|Medium
//|
//
//|{counter:codes}
//|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
//|{source_base_url}/_0474_OnesAndZeroes.java[Java]
//|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
//|Medium
//|
//

|{counter:codes}
|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
|{source_base_url}/_0474_OnesAndZeroes.java[Java]
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
|Medium
|

//|{counter:codes}
//|{leetcode_base_url}/heaters/[475. Heaters^]
//|{source_base_url}/_0475_Heaters.java[Java]
Expand Down
77 changes: 51 additions & 26 deletions docs/0474-ones-and-zeroes.adoc
Original file line number Diff line number Diff line change
@@ -1,52 +1,77 @@
[#0474-ones-and-zeroes]
= 474. Ones and Zeroes
= 474. 一和零

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

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

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`.
请你找出并返回 `+strs+` 的最大子集的长度,该子集中 *最多*`+m+``+0+``+n+``+1+`

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*.
如果 `+x+` 的所有元素也是 `+y+` 的元素,集合 `+x+` 是集合 `+y+`*子集*

*Note:*
*示例 1:*

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

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

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



*Example 1:*
*提示:*

[subs="verbatim,quotes,macros"]
----
*Input:* Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
*Output:* 4
*Explanation:* This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
----
* `+1 <= strs.length <= 600+`
* `+1 <= strs[i].length <= 100+`
* `+strs[i]+` 仅由 `+'0'+``+'1'+` 组成
* `+1 <= m, n <= 100+`

*Example 2:*
[subs="verbatim,quotes,macros"]
----
*Input:* Array = {"10", "0", "1"}, m = 1, n = 1
*Output:* 2
== 思路分析

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

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

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

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

[[src-0474]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0474_OnesAndZeroes.java[tag=answer]
----
--
// 二刷::
// +
// --
// [{java_src_attr}]
// ----
// include::{sourcedir}/_0474_OnesAndZeroes_2.java[tag=answer]
// ----
// --
====


== 参考资料

. https://leetcode.cn/problems/ones-and-zeroes/solutions/814806/yi-he-ling-by-leetcode-solution-u2z2/[474. 一和零 - 官方题解^]



2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ include::0462-minimum-moves-to-equal-array-elements-ii.adoc[leveloffset=+1]

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

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

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

Expand Down
6 changes: 6 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@
|{doc_base_url}/0031-next-permutation.adoc[题解]
|❌ 一脸懵逼,可以看成寻找下一个更大的数。这样更容易理解。
|{counter:codes}
|{leetcode_base_url}/ones-and-zeroes/[474. Ones and Zeroes^]
|{doc_base_url}/0474-ones-and-zeroes.adoc[题解]
|❌ 动态规划,0-1 背包问题。
|===
截止目前,本轮练习一共完成 {codes} 道题。
39 changes: 39 additions & 0 deletions src/main/java/com/diguage/algo/leetcode/_0474_OnesAndZeroes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.diguage.algo.leetcode;

public class _0474_OnesAndZeroes {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-10-22 19:47:11
*/
public int findMaxForm(String[] strs, int m, int n) {
int length = strs.length;
int[][] bits = new int[2][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < strs[i].length(); j++) {
char c = strs[i].charAt(j);
bits[c - '0'][i]++;
}
}
// 确定 dp 数组(dp table)以及下标的含义
// dp 数组如何初始化
int[][][] dp = new int[length + 1][m + 1][n + 1];
// 确定遍历顺序
for (int i = 1; i <= length; i++) {
int zero = bits[0][i - 1];
int one = bits[1][i - 1];
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= n; k++) {
// 确定递推公式
dp[i][j][k] = dp[i - 1][j][k];
if (j >= zero && k >= one) {
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1);
}
}
}
}
return dp[length][m][n];
}
// end::answer[]
}

0 comments on commit eb7905b

Please sign in to comment.