Skip to content

Commit 9ac3fde

Browse files
committed
Add solution 1654、1655
1 parent 0c4b373 commit 9ac3fde

16 files changed

+844
-232
lines changed

leetcode/1653.Minimum-Deletions-to-Make-String-Balanced/1653. Minimum Deletions to Make String Balanced_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,52 @@ import (
55
"testing"
66
)
77

8-
type question1649 struct {
9-
para1649
10-
ans1649
8+
type question1653 struct {
9+
para1653
10+
ans1653
1111
}
1212

1313
// para 是参数
1414
// one 代表第一个参数
15-
type para1649 struct {
15+
type para1653 struct {
1616
s string
1717
}
1818

1919
// ans 是答案
2020
// one 代表第一个答案
21-
type ans1649 struct {
21+
type ans1653 struct {
2222
one int
2323
}
2424

25-
func Test_Problem1649(t *testing.T) {
25+
func Test_Problem1653(t *testing.T) {
2626

27-
qs := []question1649{
27+
qs := []question1653{
2828

2929
{
30-
para1649{"aababbab"},
31-
ans1649{2},
30+
para1653{"aababbab"},
31+
ans1653{2},
3232
},
3333

3434
{
35-
para1649{"bbaaaaabb"},
36-
ans1649{2},
35+
para1653{"bbaaaaabb"},
36+
ans1653{2},
3737
},
3838

3939
{
40-
para1649{"b"},
41-
ans1649{0},
40+
para1653{"b"},
41+
ans1653{0},
4242
},
4343

4444
{
45-
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
46-
ans1649{25},
45+
para1653{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
46+
ans1653{25},
4747
},
4848
}
4949

50-
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
50+
fmt.Printf("------------------------Leetcode Problem 1653------------------------\n")
5151

5252
for _, q := range qs {
53-
_, p := q.ans1649, q.para1649
53+
_, p := q.ans1653, q.para1653
5454
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s))
5555
}
5656
fmt.Printf("\n\n\n")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode
2+
3+
func minimumJumps(forbidden []int, a int, b int, x int) int {
4+
visited := make([]bool, 6000)
5+
for i := range forbidden {
6+
visited[forbidden[i]] = true
7+
}
8+
queue, res := [][2]int{{0, 0}}, -1
9+
for len(queue) > 0 {
10+
length := len(queue)
11+
res++
12+
for i := 0; i < length; i++ {
13+
cur, isBack := queue[i][0], queue[i][1]
14+
if cur == x {
15+
return res
16+
}
17+
if isBack == 0 && cur-b > 0 && !visited[cur-b] {
18+
visited[cur-b] = true
19+
queue = append(queue, [2]int{cur - b, 1})
20+
}
21+
if cur+a < len(visited) && !visited[cur+a] {
22+
visited[cur+a] = true
23+
queue = append(queue, [2]int{cur + a, 0})
24+
}
25+
}
26+
queue = queue[length:]
27+
}
28+
return -1
29+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1654 struct {
9+
para1654
10+
ans1654
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1654 struct {
16+
forbidden []int
17+
a int
18+
b int
19+
x int
20+
}
21+
22+
// ans 是答案
23+
// one 代表第一个答案
24+
type ans1654 struct {
25+
one int
26+
}
27+
28+
func Test_Problem1654(t *testing.T) {
29+
30+
qs := []question1654{
31+
32+
{
33+
para1654{[]int{14, 4, 18, 1, 15}, 3, 15, 9},
34+
ans1654{3},
35+
},
36+
37+
{
38+
para1654{[]int{8, 3, 16, 6, 12, 20}, 15, 13, 11},
39+
ans1654{-1},
40+
},
41+
42+
{
43+
para1654{[]int{1, 6, 2, 14, 5, 17, 4}, 16, 9, 7},
44+
ans1654{2},
45+
},
46+
47+
{
48+
para1654{[]int{1998}, 1999, 2000, 2000},
49+
ans1654{3998},
50+
},
51+
}
52+
53+
fmt.Printf("------------------------Leetcode Problem 1654------------------------\n")
54+
55+
for _, q := range qs {
56+
_, p := q.ans1654, q.para1654
57+
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumJumps(p.forbidden, p.a, p.b, p.x))
58+
}
59+
fmt.Printf("\n\n\n")
60+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [1654. Minimum Jumps to Reach Home](https://leetcode.com/problems/minimum-jumps-to-reach-home/)
2+
3+
4+
## 题目
5+
6+
A certain bug's home is on the x-axis at position `x`. Help them get there from position `0`.
7+
8+
The bug jumps according to the following rules:
9+
10+
- It can jump exactly `a` positions **forward** (to the right).
11+
- It can jump exactly `b` positions **backward** (to the left).
12+
- It cannot jump backward twice in a row.
13+
- It cannot jump to any `forbidden` positions.
14+
15+
The bug may jump forward **beyond** its home, but it **cannot jump** to positions numbered with **negative** integers.
16+
17+
Given an array of integers `forbidden`, where `forbidden[i]` means that the bug cannot jump to the position `forbidden[i]`, and integers `a``b`, and `x`, return *the minimum number of jumps needed for the bug to reach its home*. If there is no possible sequence of jumps that lands the bug on position `x`, return `1.`
18+
19+
**Example 1:**
20+
21+
```
22+
Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
23+
Output: 3
24+
Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
31+
Output: -1
32+
```
33+
34+
**Example 3:**
35+
36+
```
37+
Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
38+
Output: 2
39+
Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
40+
41+
```
42+
43+
**Constraints:**
44+
45+
- `1 <= forbidden.length <= 1000`
46+
- `1 <= a, b, forbidden[i] <= 2000`
47+
- `0 <= x <= 2000`
48+
- All the elements in `forbidden` are distinct.
49+
- Position `x` is not forbidden.
50+
51+
## 题目大意
52+
53+
有一只跳蚤的家在数轴上的位置 x 处。请你帮助它从位置 0 出发,到达它的家。
54+
55+
跳蚤跳跃的规则如下:
56+
57+
- 它可以 往前 跳恰好 a 个位置(即往右跳)。
58+
- 它可以 往后 跳恰好 b 个位置(即往左跳)。
59+
- 它不能 连续 往后跳 2 次。
60+
- 它不能跳到任何 forbidden 数组中的位置。
61+
62+
跳蚤可以往前跳 超过 它的家的位置,但是它 不能跳到负整数 的位置。给你一个整数数组 forbidden ,其中 forbidden[i] 是跳蚤不能跳到的位置,同时给你整数 a, b 和 x ,请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 x 的可行方案,请你返回 -1 。
63+
64+
## 解题思路
65+
66+
- 给出坐标 x ,可以往前跳的步长 a,往后跳的步长 b。要求输出能跳回家的最少跳跃次数。
67+
- 求最少跳跃次数,思路用 BFS 求解,最先到达坐标 x 的方案即是最少跳跃次数。对`forbidden` 的处理是把记忆化数组里面把他们标记为 true。禁止连续往后跳 2 次的限制,要求我们在 BFS 入队的时候再记录一下跳跃方向,每次往后跳的时候判断前一跳是否是往后跳,如果是往后跳,此次就不能往后跳了。
68+
69+
## 代码
70+
71+
```go
72+
package leetcode
73+
74+
func minimumJumps(forbidden []int, a int, b int, x int) int {
75+
visited := make([]bool, 6000)
76+
for i := range forbidden {
77+
visited[forbidden[i]] = true
78+
}
79+
queue, res := [][2]int{{0, 0}}, -1
80+
for len(queue) > 0 {
81+
length := len(queue)
82+
res++
83+
for i := 0; i < length; i++ {
84+
cur, isBack := queue[i][0], queue[i][1]
85+
if cur == x {
86+
return res
87+
}
88+
if isBack == 0 && cur-b > 0 && !visited[cur-b] {
89+
visited[cur-b] = true
90+
queue = append(queue, [2]int{cur - b, 1})
91+
}
92+
if cur+a < len(visited) && !visited[cur+a] {
93+
visited[cur+a] = true
94+
queue = append(queue, [2]int{cur + a, 0})
95+
}
96+
}
97+
queue = queue[length:]
98+
}
99+
return -1
100+
}
101+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
func canDistribute(nums []int, quantity []int) bool {
4+
freq := make(map[int]int)
5+
for _, n := range nums {
6+
freq[n]++
7+
}
8+
return dfs(freq, quantity)
9+
}
10+
11+
func dfs(freq map[int]int, quantity []int) bool {
12+
if len(quantity) == 0 {
13+
return true
14+
}
15+
visited := make(map[int]bool)
16+
for i := range freq {
17+
if visited[freq[i]] {
18+
continue
19+
}
20+
visited[freq[i]] = true
21+
if freq[i] >= quantity[0] {
22+
freq[i] -= quantity[0]
23+
if dfs(freq, quantity[1:]) {
24+
return true
25+
}
26+
freq[i] += quantity[0]
27+
}
28+
}
29+
return false
30+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1655 struct {
9+
para1655
10+
ans1655
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1655 struct {
16+
nums []int
17+
quantity []int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1655 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem1655(t *testing.T) {
27+
28+
qs := []question1655{
29+
30+
{
31+
para1655{[]int{1, 2, 3, 4}, []int{2}},
32+
ans1655{false},
33+
},
34+
35+
{
36+
para1655{[]int{1, 2, 3, 3}, []int{2}},
37+
ans1655{true},
38+
},
39+
40+
{
41+
para1655{[]int{1, 1, 2, 2}, []int{2, 2}},
42+
ans1655{true},
43+
},
44+
45+
{
46+
para1655{[]int{1, 1, 2, 3}, []int{2, 2}},
47+
ans1655{false},
48+
},
49+
50+
{
51+
para1655{[]int{1, 1, 1, 1, 1}, []int{2, 3}},
52+
ans1655{true},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 1655------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans1655, q.para1655
60+
fmt.Printf("【input】:%v 【output】:%v \n", p, canDistribute(p.nums, p.quantity))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}

0 commit comments

Comments
 (0)