Skip to content

Commit cd23cc2

Browse files
committed
#276 Jump Game
1 parent 15c99cd commit cd23cc2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

jump-game/forest000014.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
# Time Complexity: O(n)
3+
# Space Complexity: O(n)
4+
*/
5+
6+
class Solution {
7+
public boolean canJump(int[] nums) {
8+
int n = nums.length;
9+
boolean[] dp = new boolean[n];
10+
11+
dp[0] = true;
12+
13+
for (int i = 0; i < n; i++) {
14+
if (!dp[i]) return false;
15+
int j = Math.min(n - 1, i + nums[i]);
16+
for (; j >= i + 1; j--) {
17+
if (dp[j]) break;
18+
dp[j] = true;
19+
}
20+
}
21+
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)