42 lines
967 B
Java
42 lines
967 B
Java
/*
|
|
* @lc app=leetcode id=45 lang=java
|
|
*
|
|
* [45] Jump Game II
|
|
*/
|
|
|
|
// @lc code=start
|
|
class Solution {
|
|
public int jump(int[] nums) {
|
|
// create array and zset
|
|
int[] dp = new int[nums.length];
|
|
zset(dp, -1);
|
|
dp[0] = 0;
|
|
for(int i=0; i<nums.length; i++) {
|
|
int w = nums[i];
|
|
if(dp[i] < 0) {
|
|
// unreachable
|
|
continue;
|
|
}
|
|
for(int j= i+1; j<=i+w && j < nums.length; j++) {
|
|
if(dp[j] < 0) {
|
|
dp[j] = dp[i] + 1;
|
|
} else {
|
|
dp[j] = Math.min(dp[j], dp[i] + 1);
|
|
}
|
|
}
|
|
}
|
|
return dp[nums.length-1];
|
|
}
|
|
|
|
private void zset(int[] array, int v) {
|
|
if(array == null) {
|
|
return;
|
|
}
|
|
for(int i=0;i<array.length; i++) {
|
|
array[i] = v;
|
|
}
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|