Skip to content

Commit 37fb6e2

Browse files
committed
jump-game
1 parent ed38bd6 commit 37fb6e2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

jump-game/chjung99.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
boolean isArrived;
3+
int SIZE;
4+
5+
public boolean canJump(int[] nums) {
6+
isArrived = false;
7+
SIZE = nums.length;
8+
9+
bfs(nums, 0);
10+
11+
return isArrived;
12+
}
13+
14+
public void bfs(int[] nums, int x) {
15+
Queue<Integer> q = new ArrayDeque<>();
16+
q.add(x);
17+
boolean[] visit = new boolean[SIZE];
18+
visit[x] = true;
19+
20+
int curX;
21+
int nextX;
22+
23+
while (!q.isEmpty()){
24+
curX = q.poll();
25+
isArrived = (curX + 1) == SIZE;
26+
if (isArrived) return;
27+
28+
for (int dx = 1; dx <= nums[curX]; dx++){
29+
nextX = curX + dx;
30+
if (nextX >= SIZE || visit[nextX]) continue;
31+
q.add(nextX);
32+
visit[nextX] = true;
33+
}
34+
}
35+
}
36+
}
37+
38+

0 commit comments

Comments
 (0)