2025-05-17提交

This commit is contained in:
asahi
2025-05-17 17:01:56 +08:00
parent c4e1e74677
commit a62543b3fd
7 changed files with 454 additions and 0 deletions

41
45.jump-game-ii.java Normal file
View 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