Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcbeeb41

Browse files
authored
Merge pull request6boris#454 from 0xff-dev/1345
Add solution and test-cases for problem 1345
2 parentsd0da33f +2665bc0 commitcbeeb41

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

‎leetcode/1301-1400/1345.Jump-Game-IV/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
#[1345.Jump Game IV][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
Given an array of integers`arr`, you are initially positioned at the first index of the array.
5+
6+
In one step you can jump from index i to index:
7+
8+
-`i + 1` where:`i + 1 < arr.length`.
9+
-`i - 1` where:`i - 1 >= 0`.
10+
-`j` where:`arr[i] == arr[j]` and`i != j`.
11+
12+
Return the minimum number of steps to reach the**last index** of the array.
13+
14+
Notice that you can not jump outside of the array at any time.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
20+
Output: 3
21+
Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
1322
```
1423

15-
##题意
16-
>...
24+
**Example 2:**
1725

18-
##题解
19-
20-
###思路1
21-
>...
22-
Jump Game IV
23-
```go
2426
```
27+
Input: arr = [7]
28+
Output: 0
29+
Explanation: Start index is the last index. You do not need to jump.
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: arr = [7,6,9,6,9,6,9,7]
36+
Output: 1
37+
Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
38+
```
2639

2740
##结语
2841

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(arr []int)int {
4+
length:=len(arr)
5+
iflength<=1 {
6+
return0
7+
}
8+
// bfs
9+
step:=0
10+
indices:=make(map[int][]int)
11+
visited:=make([]bool,length)
12+
fori:=0;i<length;i++ {
13+
if_,ok:=indices[arr[i]];!ok {
14+
indices[arr[i]]=make([]int,0)
15+
}
16+
indices[arr[i]]=append(indices[arr[i]],i)
17+
}
18+
visited[0]=true
19+
queue:= []int{0}
20+
21+
forlen(queue)>0 {
22+
nextQ:=make([]int,0)
23+
for_,index:=rangequeue {
24+
ifindex-1>=0&&!visited[index-1] {
25+
visited[index-1]=true
26+
nextQ=append(nextQ,index-1)
27+
}
28+
ifindex+1<length&&!visited[index+1] {
29+
visited[index+1]=true
30+
nextQ=append(nextQ,index+1)
31+
}
32+
for_,rel:=rangeindices[arr[index]] {
33+
if!visited[rel] {
34+
visited[rel]=true
35+
nextQ=append(nextQ,rel)
36+
}
37+
}
38+
// 注意这里的超时点
39+
delete(indices,arr[index])
40+
}
41+
step++
42+
ifvisited[length-1] {
43+
returnstep
44+
}
45+
queue=nextQ
46+
47+
}
48+
return0
549
}

‎leetcode/1301-1400/1345.Jump-Game-IV/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
inputs[]int
14+
expectint
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase1",[]int{100,-23,-23,404,100,23,23,23,3,404},3},
17+
{"TestCase2",[]int{7},0},
18+
{"TestCase3",[]int{7,6,9,6,9,6,9,7},1},
1919
}
2020

2121
//开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
funcBenchmarkSolution(b*testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
funcExampleSolution() {
3939
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp