Jump Game
Leetcode https://leetcode.cn/problems/jump-game/
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
class Solution {
public boolean canJump(int[] nums) {
if (nums == null || nums.length == 0) {
return false;
}
int n = nums.length;
boolean[] canHop = new boolean[n];
canHop[n-1] = true;
for (int i = n - 2; i >= 0; i--) {
if (i + nums[i] >= n - 1) {
canHop[i] = true;
} else {
for (int j = 1; j <= nums[i]; j++) {
if (canHop[i + j]) {
canHop[i] = true;
break;
}
}
}
}
return canHop[0];
}
}
Complexity
- Time = O(n^2)
- Space = O(n)
This post is licensed under CC BY 4.0 by the author.