2024/9/22 LeetCode Hot100 贪心
This commit is contained in:
parent
515c15677d
commit
31d7c61ae9
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue