Skip to content

Commit 9a30c00

Browse files
committed
Add solution 0583、1482
1 parent 09c6e47 commit 9a30c00

24 files changed

+750
-182
lines changed

README.md

Lines changed: 149 additions & 145 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode
2+
3+
func minDistance(word1 string, word2 string) int {
4+
dp := make([][]int, len(word1)+1)
5+
for i := 0; i < len(word1)+1; i++ {
6+
dp[i] = make([]int, len(word2)+1)
7+
}
8+
for i := 0; i < len(word1)+1; i++ {
9+
dp[i][0] = i
10+
}
11+
for i := 0; i < len(word2)+1; i++ {
12+
dp[0][i] = i
13+
}
14+
for i := 1; i < len(word1)+1; i++ {
15+
for j := 1; j < len(word2)+1; j++ {
16+
if word1[i-1] == word2[j-1] {
17+
dp[i][j] = dp[i-1][j-1]
18+
} else {
19+
dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j])
20+
}
21+
}
22+
}
23+
return dp[len(word1)][len(word2)]
24+
}
25+
26+
func min(x, y int) int {
27+
if x < y {
28+
return x
29+
}
30+
return y
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question583 struct {
9+
para583
10+
ans583
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para583 struct {
16+
word1 string
17+
word2 string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans583 struct {
23+
one int
24+
}
25+
26+
func Test_Problem583(t *testing.T) {
27+
28+
qs := []question583{
29+
30+
{
31+
para583{"sea", "eat"},
32+
ans583{2},
33+
},
34+
35+
{
36+
para583{"leetcode", "etco"},
37+
ans583{4},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 583------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans583, q.para583
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, minDistance(p.word1, p.word2))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [583. Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)
2+
3+
4+
## 题目
5+
6+
Given two strings `word1` and `word2`, return *the minimum number of **steps** required to make* `word1` *and* `word2` *the same*.
7+
8+
In one **step**, you can delete exactly one character in either string.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: word1 = "sea", word2 = "eat"
14+
Output: 2
15+
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: word1 = "leetcode", word2 = "etco"
22+
Output: 4
23+
```
24+
25+
**Constraints:**
26+
27+
- `1 <= word1.length, word2.length <= 500`
28+
- `word1` and `word2` consist of only lowercase English letters.
29+
30+
## 题目大意
31+
32+
给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
33+
34+
## 解题思路
35+
36+
- 从题目数据量级判断,此题一定是 O(n^2) 动态规划题。定义 `dp[i][j]` 表示 `word1[:i]``word2[:j]` 匹配所删除的最少步数。如果 `word1[:i-1]``word2[:j-1]` 匹配,那么 `dp[i][j] = dp[i-1][j-1]`。如果 `word1[:i-1]``word2[:j-1]` 不匹配,那么需要考虑删除一次,所以 `dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j])`。所以动态转移方程是:
37+
38+
$$dp[i][j] = \left\{\begin{matrix}dp[i-1][j-1]&, word1[i-1] == word2[j-1]\\ 1 + min(dp[i][j-1], dp[i-1][j])&, word1[i-1] \neq word2[j-1]\\\end{matrix}\right.$$
39+
40+
最终答案存储在 `dp[len(word1)][len(word2)]` 中。
41+
42+
## 代码
43+
44+
```go
45+
package leetcode
46+
47+
func minDistance(word1 string, word2 string) int {
48+
dp := make([][]int, len(word1)+1)
49+
for i := 0; i < len(word1)+1; i++ {
50+
dp[i] = make([]int, len(word2)+1)
51+
}
52+
for i := 0; i < len(word1)+1; i++ {
53+
dp[i][0] = i
54+
}
55+
for i := 0; i < len(word2)+1; i++ {
56+
dp[0][i] = i
57+
}
58+
for i := 1; i < len(word1)+1; i++ {
59+
for j := 1; j < len(word2)+1; j++ {
60+
if word1[i-1] == word2[j-1] {
61+
dp[i][j] = dp[i-1][j-1]
62+
} else {
63+
dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j])
64+
}
65+
}
66+
}
67+
return dp[len(word1)][len(word2)]
68+
}
69+
70+
func min(x, y int) int {
71+
if x < y {
72+
return x
73+
}
74+
return y
75+
}
76+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func minDays(bloomDay []int, m int, k int) int {
6+
if m*k > len(bloomDay) {
7+
return -1
8+
}
9+
maxDay := 0
10+
for _, day := range bloomDay {
11+
if day > maxDay {
12+
maxDay = day
13+
}
14+
}
15+
return sort.Search(maxDay, func(days int) bool {
16+
flowers, bouquets := 0, 0
17+
for _, d := range bloomDay {
18+
if d > days {
19+
flowers = 0
20+
} else {
21+
flowers++
22+
if flowers == k {
23+
bouquets++
24+
flowers = 0
25+
}
26+
}
27+
}
28+
return bouquets >= m
29+
})
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1482 struct {
9+
para1482
10+
ans1482
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1482 struct {
16+
bloomDay []int
17+
m int
18+
k int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans1482 struct {
24+
one int
25+
}
26+
27+
func Test_Problem1482(t *testing.T) {
28+
29+
qs := []question1482{
30+
31+
{
32+
para1482{[]int{1, 10, 3, 10, 2}, 3, 1},
33+
ans1482{3},
34+
},
35+
36+
{
37+
para1482{[]int{1, 10, 3, 10, 2}, 3, 2},
38+
ans1482{-1},
39+
},
40+
41+
{
42+
para1482{[]int{7, 7, 7, 7, 12, 7, 7}, 2, 3},
43+
ans1482{12},
44+
},
45+
46+
{
47+
para1482{[]int{1000000000, 1000000000}, 1, 1},
48+
ans1482{1000000000},
49+
},
50+
51+
{
52+
para1482{[]int{1, 10, 2, 9, 3, 8, 4, 7, 5, 6}, 4, 2},
53+
ans1482{9},
54+
},
55+
}
56+
57+
fmt.Printf("------------------------Leetcode Problem 1482------------------------\n")
58+
59+
for _, q := range qs {
60+
_, p := q.ans1482, q.para1482
61+
fmt.Printf("【input】:%v 【output】:%v \n", p, minDays(p.bloomDay, p.m, p.k))
62+
}
63+
fmt.Printf("\n\n\n")
64+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# [1482. Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/)
2+
3+
## 题目
4+
5+
Given an integer array `bloomDay`, an integer `m` and an integer `k`.
6+
7+
We need to make `m` bouquets. To make a bouquet, you need to use `k` **adjacent flowers** from the garden.
8+
9+
The garden consists of `n` flowers, the `ith` flower will bloom in the `bloomDay[i]` and then can be used in **exactly one** bouquet.
10+
11+
Return *the minimum number of days* you need to wait to be able to make `m` bouquets from the garden. If it is impossible to make `m` bouquets return **-1**.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
17+
Output: 3
18+
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
19+
We need 3 bouquets each should contain 1 flower.
20+
After day 1: [x, _, _, _, _] // we can only make one bouquet.
21+
After day 2: [x, _, _, _, x] // we can only make two bouquets.
22+
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
29+
Output: -1
30+
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
37+
Output: 12
38+
Explanation: We need 2 bouquets each should have 3 flowers.
39+
Here's the garden after the 7 and 12 days:
40+
After day 7: [x, x, x, x, _, x, x]
41+
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
42+
After day 12: [x, x, x, x, x, x, x]
43+
It is obvious that we can make two bouquets in different ways.
44+
```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
50+
Output: 1000000000
51+
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
58+
Output: 9
59+
```
60+
61+
**Constraints:**
62+
63+
- `bloomDay.length == n`
64+
- `1 <= n <= 10^5`
65+
- `1 <= bloomDay[i] <= 10^9`
66+
- `1 <= m <= 10^6`
67+
- `1 <= k <= n`
68+
69+
## 题目大意
70+
71+
给你一个整数数组 bloomDay,以及两个整数 m 和 k 。现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。
72+
73+
## 解题思路
74+
75+
- 本题是二分搜索提醒。题目解空间固定,答案区间一定在 [0, maxDay] 中。这是单调增且有序区间,所以可以在这个解空间内使用二分搜索。在区间 [0, maxDay] 中找到第一个能满足 m 束花的解。二分搜索判断是否为 true 的条件为:从左往右遍历数组,依次统计当前日期下,花是否开了,如果连续开花 k 朵,便为 1 束,数组遍历结束如果花束总数 ≥ k 即为答案。二分搜索会返回最小的下标,即对应满足题意的最少天数。
76+
77+
## 代码
78+
79+
```go
80+
package leetcode
81+
82+
import "sort"
83+
84+
func minDays(bloomDay []int, m int, k int) int {
85+
if m*k > len(bloomDay) {
86+
return -1
87+
}
88+
maxDay := 0
89+
for _, day := range bloomDay {
90+
if day > maxDay {
91+
maxDay = day
92+
}
93+
}
94+
return sort.Search(maxDay, func(days int) bool {
95+
flowers, bouquets := 0, 0
96+
for _, d := range bloomDay {
97+
if d > days {
98+
flowers = 0
99+
} else {
100+
flowers++
101+
if flowers == k {
102+
bouquets++
103+
flowers = 0
104+
}
105+
}
106+
}
107+
return bouquets >= m
108+
})
109+
}
110+
```

website/content/ChapterFour/0500~0599/0581.Shortest-Unsorted-Continuous-Subarray.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ func min(a, b int) int {
110110
----------------------------------------------
111111
<div style="display: flex;justify-content: space-between;align-items: center;">
112112
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0575.Distribute-Candies/">⬅️上一页</a></p>
113-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0589.N-ary-Tree-Preorder-Traversal/">下一页➡️</a></p>
113+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0583.Delete-Operation-for-Two-Strings/">下一页➡️</a></p>
114114
</div>

0 commit comments

Comments
 (0)