-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
34 lines (29 loc) · 818 Bytes
/
JumpGame.java
File metadata and controls
34 lines (29 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class JumpGame {
public boolean canJump(int[] nums) {
int reachable = 0;
if (nums.length < 2) {
return true;
}
if (nums[0] == 0) {
return false;
}
for (int i = 0; i < nums.length; i++) {
int jumpedPosition = nums[i] + i;
if (i > reachable) {
return false;
}
if (jumpedPosition > reachable) {
reachable = jumpedPosition;
}
if (jumpedPosition >= nums.length -1) {
return true;
}
}
return false;
}
public static void main(String[] args) {
JumpGame jumpGame = new JumpGame();
int[] nums = {3,2,1,0,4};
System.out.println(jumpGame.canJump(nums));
}
}