Skip to content

Commit 2db5ab0

Browse files
committed
添加 problem 474
1 parent b78d2c5 commit 2db5ab0

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
func findMaxForm(strs []string, m int, n int) int {
6+
dp := make([][]int, m+1)
7+
for i := 0; i < m+1; i++ {
8+
dp[i] = make([]int, n+1)
9+
}
10+
for _, s := range strs {
11+
zero := strings.Count(s, "0")
12+
one := len(s) - zero
13+
if zero > m || one > n {
14+
continue
15+
}
16+
for i := m; i >= zero; i-- {
17+
for j := n; j >= one; j-- {
18+
dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one])
19+
}
20+
}
21+
}
22+
return dp[m][n]
23+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question474 struct {
9+
para474
10+
ans474
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para474 struct {
16+
strs []string
17+
m int
18+
n int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans474 struct {
24+
one int
25+
}
26+
27+
func Test_Problem474(t *testing.T) {
28+
29+
qs := []question474{
30+
31+
question474{
32+
para474{[]string{"10", "0001", "111001", "1", "0"}, 5, 3},
33+
ans474{4},
34+
},
35+
36+
question474{
37+
para474{[]string{"10", "0", "1"}, 1, 1},
38+
ans474{2},
39+
},
40+
41+
question474{
42+
para474{[]string{}, 0, 0},
43+
ans474{0},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 474------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans474, q.para474
51+
fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxForm(p.strs, p.m, p.n))
52+
}
53+
fmt.Printf("\n\n\n")
54+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [474. Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/)
2+
3+
4+
## 题目:
5+
6+
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
7+
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`.
9+
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**.
11+
12+
**Note:**
13+
14+
1. The given numbers of `0s` and `1s` will both not exceed `100`
15+
2. The size of given string array won't exceed `600`.
16+
17+
**Example 1:**
18+
19+
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
20+
Output: 4
21+
22+
Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
23+
24+
**Example 2:**
25+
26+
Input: Array = {"10", "0", "1"}, m = 1, n = 1
27+
Output: 2
28+
29+
Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
30+
31+
32+
## 题目大意
33+
34+
在计算机界中,我们总是追求用有限的资源获取最大的收益。现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。
35+
36+
注意:
37+
38+
1. 给定 0 和 1 的数量都不会超过 100。
39+
2. 给定字符串数组的长度不会超过 600。
40+
41+
42+
43+
## 解题思路
44+
45+
- 给定一个字符串数组和 m,n,其中所有的字符都是由 0 和 1 组成的。问能否从数组中取出最多的字符串,使得这些取出的字符串中所有的 0 的个数 ≤ m,1 的个数 ≤ n。
46+
- 这一题是典型的 01 背包的题型。只不过是一个二维的背包问题。在 n 个物品中选出一定物品,**尽量完全填满** m 维和 n 维的背包。为什么是尽量填满?因为不一定能完全填满背包。
47+
- `dp[i][j]` 代表尽量填满容量为 `(i,j)` 的背包装下的物品总数,状态转移方程为 `dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one])`。其中 zero 代表的当前装入物品在 m 维上的体积,也即 0 的个数。one 代表的是当前装入物品在 n 维上的体积,也即 1 的个数。每添加一个物品,比较当前 (i,j) 的背包装下的物品总数和 (i-zero,j-one) 的背包装下的物品总数 + 1,比较这两者的大小,保存两者的最大值。每添加一个物品就刷新这个二维背包,直到所有物品都扫完一遍。dp[m][n] 中存储的就是最终的答案。时间复杂度 `O( n * M * N )`

0 commit comments

Comments
 (0)