2025-05-17提交
This commit is contained in:
41
45.jump-game-ii.java
Normal file
41
45.jump-game-ii.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @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
|
||||
|
||||
Reference in New Issue
Block a user