-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathJumpGame.java
More file actions
25 lines (18 loc) · 583 Bytes
/
Copy pathJumpGame.java
File metadata and controls
25 lines (18 loc) · 583 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
package leetcode.medium;
public class JumpGame {
boolean canJump(int[] nums) {
// Initially the final position is the last index
int finalPosition = nums.length - 1;
// Start with the second last index
for (int idx = nums.length - 2; idx >= 0; idx--) {
// If you can reach the final position from this index
// update the final position flag
if (idx + nums[idx] >= finalPosition) {
finalPosition = idx;
}
}
// If we reach the first index, then we can
// make the jump possible
return finalPosition == 0;
}
}