-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path55.jump-game.java
69 lines (67 loc) · 1.44 KB
/
55.jump-game.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode id=55 lang=java
*
* [55] Jump Game
*
* https://leetcode.com/problems/jump-game/description/
*
* algorithms
* Medium (31.76%)
* Likes: 2179
* Dislikes: 213
* Total Accepted: 287.2K
* Total Submissions: 888.2K
* Testcase Example: '[2,3,1,1,4]'
*
* Given an array of non-negative integers, you are initially positioned at the
* first index of the array.
*
* Each element in the array represents your maximum jump length at that
* position.
*
* Determine if you are able to reach the last index.
*
* Example 1:
*
*
* Input: [2,3,1,1,4]
* Output: true
* Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last
* index.
*
*
* Example 2:
*
*
* Input: [3,2,1,0,4]
* Output: false
* Explanation: You will always arrive at index 3 no matter what. Its
* maximum
* jump length is 0, which makes it impossible to reach the last index.
*
*
*/
class Solution {
public boolean canJump(int[] nums) {
int[] dp = new int[nums.length];
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] == 0) {
dp[i] = i;
} else if (i + nums[i] >= nums.length - 1) {
dp[i] = nums.length - 1;
} else {
int max = 0;
for (int j = i + 1; j <= i + nums[i]; j++) {
if (dp[j] > max) {
max = dp[j];
}
}
dp[i] = max;
}
}
if (dp[0] == nums.length - 1) {
return true;
}
return false;
}
}