2024/9/22 LeetCode Hot100 贪心

This commit is contained in:
Cool 2024-09-23 00:05:35 +08:00
parent 515c15677d
commit 31d7c61ae9
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.cool.hot100.greedy;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/22/19:47
* DayNumber 1
* Hard 2
* Level 5
*/
public class Num55 {
public boolean canJump(int[] nums) {
int len=nums.length;
int maxDistance=0;
for(int i=0;i<len;i++){
if(i<=maxDistance){
maxDistance=Math.max(maxDistance,nums[i]+i);
if(maxDistance>=len-1){
return true;
}
}
}
return false;
}
/**
* @Author Cool
* @Date 20:01 2024/9/22
* 倒序遍历版
**/
public boolean canJump1(int[] nums) {
int step = 1;
for (int i = nums.length - 2; i > -1; i--) {
if (nums[i] >= step) {
step = 0;
}
step++;
}
return step == 1;
}
}