File tree 5 files changed +253
-1
lines changed
5 files changed +253
-1
lines changed Original file line number Diff line number Diff line change 29
29
30
30
<!-- 这里可写通用的实现逻辑 -->
31
31
32
+ ** 方法一:BFS**
33
+
32
34
<!-- tabs:start -->
33
35
34
36
### ** Python3**
35
37
36
38
<!-- 这里可写当前语言的特殊实现逻辑 -->
37
39
38
40
``` python
39
-
41
+ class Solution :
42
+ def minJump (self , jump : List[int ]) -> int :
43
+ n = len (jump)
44
+ vis = [False ] * (n + 1 )
45
+ q = deque([0 ])
46
+ ans = 0
47
+ vis[0 ] = True
48
+ mx = 1
49
+ while q:
50
+ for _ in range (len (q)):
51
+ i = q.popleft()
52
+ if i + jump[i] >= n:
53
+ return ans + 1
54
+ for j in list (range (mx, i)) + [i + jump[i]]:
55
+ if not vis[j]:
56
+ q.append(j)
57
+ vis[j] = True
58
+ mx = max (mx, i + 1 )
59
+ ans += 1
60
+ return - 1
40
61
```
41
62
42
63
### ** Java**
43
64
44
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
45
66
46
67
``` java
68
+ class Solution {
69
+ public int minJump (int [] jump ) {
70
+ int n = jump. length;
71
+ boolean [] vis = new boolean [n + 1 ];
72
+ Deque<Integer > q = new ArrayDeque<> ();
73
+ q. offer(0 );
74
+ vis[0 ] = true ;
75
+ int ans = 0 ;
76
+ int mx = 1 ;
77
+ while (! q. isEmpty()) {
78
+ for (int t = q. size(); t > 0 ; -- t) {
79
+ int i = q. poll();
80
+ int j = i + jump[i];
81
+ if (j >= n) {
82
+ return ans + 1 ;
83
+ }
84
+ if (! vis[j]) {
85
+ q. offer(j);
86
+ vis[j] = true ;
87
+ }
88
+ for (j = mx; j < i; ++ j) {
89
+ if (! vis[j]) {
90
+ q. offer(j);
91
+ vis[j] = true ;
92
+ }
93
+ }
94
+ mx = Math . max(mx, i + 1 );
95
+ }
96
+ ++ ans;
97
+ }
98
+ return - 1 ;
99
+ }
100
+ }
101
+ ```
102
+
103
+ ### ** C++**
104
+
105
+ ``` cpp
106
+ class Solution {
107
+ public:
108
+ int minJump(vector<int >& jump) {
109
+ int n = jump.size();
110
+ vector<bool > vis(n + 1);
111
+ queue<int > q{{0}};
112
+ vis[ 0] = true;
113
+ int ans = 0, mx = 1;
114
+ while (!q.empty())
115
+ {
116
+ for (int t = q.size(); t; --t)
117
+ {
118
+ int i = q.front();
119
+ int j = i + jump[ i] ;
120
+ if (j >= n) return ans + 1;
121
+ q.pop();
122
+ if (!vis[ j] )
123
+ {
124
+ vis[ j] = true;
125
+ q.push(j);
126
+ }
127
+ for (j = mx; j < i; ++j)
128
+ {
129
+ if (!vis[ j] )
130
+ {
131
+ vis[ j] = true;
132
+ q.push(j);
133
+ }
134
+ }
135
+ mx = max(mx, i + 1);
136
+ }
137
+ ++ans;
138
+ }
139
+ return -1;
140
+ }
141
+ };
142
+ ```
47
143
144
+ ### **Go**
145
+
146
+ ```go
147
+ func minJump(jump []int) int {
148
+ n := len(jump)
149
+ vis := make([]bool, n + 1)
150
+ q := []int{0}
151
+ vis[0] = true
152
+ ans, mx := 0, 1
153
+ for len(q) > 0 {
154
+ for t := len(q); t > 0; t-- {
155
+ i := q[0]
156
+ q = q[1:]
157
+ j := i + jump[i]
158
+ if j >= n {
159
+ return ans + 1
160
+ }
161
+ if !vis[j] {
162
+ vis[j] = true
163
+ q = append(q, j)
164
+ }
165
+ for j = mx; j < i; j++ {
166
+ if !vis[j] {
167
+ vis[j] = true
168
+ q = append(q, j)
169
+ }
170
+ }
171
+ if mx < i + 1 {
172
+ mx = i + 1
173
+ }
174
+ }
175
+ ans++
176
+ }
177
+ return -1
178
+ }
48
179
```
49
180
50
181
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minJump (vector<int >& jump) {
4
+ int n = jump.size ();
5
+ vector<bool > vis (n + 1 );
6
+ queue<int > q{{0 }};
7
+ vis[0 ] = true ;
8
+ int ans = 0 , mx = 1 ;
9
+ while (!q.empty ())
10
+ {
11
+ for (int t = q.size (); t; --t)
12
+ {
13
+ int i = q.front ();
14
+ int j = i + jump[i];
15
+ if (j >= n) return ans + 1 ;
16
+ q.pop ();
17
+ if (!vis[j])
18
+ {
19
+ vis[j] = true ;
20
+ q.push (j);
21
+ }
22
+ for (j = mx; j < i; ++j)
23
+ {
24
+ if (!vis[j])
25
+ {
26
+ vis[j] = true ;
27
+ q.push (j);
28
+ }
29
+ }
30
+ mx = max (mx, i + 1 );
31
+ }
32
+ ++ans;
33
+ }
34
+ return -1 ;
35
+ }
36
+ };
Original file line number Diff line number Diff line change
1
+ func minJump (jump []int ) int {
2
+ n := len (jump )
3
+ vis := make ([]bool , n + 1 )
4
+ q := []int {0 }
5
+ vis [0 ] = true
6
+ ans , mx := 0 , 1
7
+ for len (q ) > 0 {
8
+ for t := len (q ); t > 0 ; t -- {
9
+ i := q [0 ]
10
+ q = q [1 :]
11
+ j := i + jump [i ]
12
+ if j >= n {
13
+ return ans + 1
14
+ }
15
+ if ! vis [j ] {
16
+ vis [j ] = true
17
+ q = append (q , j )
18
+ }
19
+ for j = mx ; j < i ; j ++ {
20
+ if ! vis [j ] {
21
+ vis [j ] = true
22
+ q = append (q , j )
23
+ }
24
+ }
25
+ if mx < i + 1 {
26
+ mx = i + 1
27
+ }
28
+ }
29
+ ans ++
30
+ }
31
+ return - 1
32
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minJump (int [] jump ) {
3
+ int n = jump .length ;
4
+ boolean [] vis = new boolean [n + 1 ];
5
+ Deque <Integer > q = new ArrayDeque <>();
6
+ q .offer (0 );
7
+ vis [0 ] = true ;
8
+ int ans = 0 ;
9
+ int mx = 1 ;
10
+ while (!q .isEmpty ()) {
11
+ for (int t = q .size (); t > 0 ; --t ) {
12
+ int i = q .poll ();
13
+ int j = i + jump [i ];
14
+ if (j >= n ) {
15
+ return ans + 1 ;
16
+ }
17
+ if (!vis [j ]) {
18
+ q .offer (j );
19
+ vis [j ] = true ;
20
+ }
21
+ for (j = mx ; j < i ; ++j ) {
22
+ if (!vis [j ]) {
23
+ q .offer (j );
24
+ vis [j ] = true ;
25
+ }
26
+ }
27
+ mx = Math .max (mx , i + 1 );
28
+ }
29
+ ++ans ;
30
+ }
31
+ return -1 ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minJump (self , jump : List [int ]) -> int :
3
+ n = len (jump )
4
+ vis = [False ] * (n + 1 )
5
+ q = deque ([0 ])
6
+ ans = 0
7
+ vis [0 ] = True
8
+ mx = 1
9
+ while q :
10
+ for _ in range (len (q )):
11
+ i = q .popleft ()
12
+ if i + jump [i ] >= n :
13
+ return ans + 1
14
+ for j in list (range (mx , i )) + [i + jump [i ]]:
15
+ if not vis [j ]:
16
+ q .append (j )
17
+ vis [j ] = True
18
+ mx = max (mx , i + 1 )
19
+ ans += 1
20
+ return - 1
You can’t perform that action at this time.
0 commit comments