2024/9/23 LeetCode Hot100 贪心结束

This commit is contained in:
Cool 2024-09-23 22:18:58 +08:00
parent 31d7c61ae9
commit feda21c8d6
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.cool.hot100.greedy;
import org.junit.Test;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/23/20:47
* DayNumber 1
* Hard 2
* Level 6
*/
public class Num45 {
public int jump(int[] nums) {
int end=0;
int maxDistance=0;
int res=0;
for(int i=0;i<nums.length-1;i++){
maxDistance=Math.max(maxDistance,i+nums[i]);
if(i==end){
end=maxDistance;
res++;
}
}
return res;
}
@Test
public void test(){
jump(new int[]{1,2,3,999});
}
}

View File

@ -0,0 +1,36 @@
package com.cool.hot100.greedy;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/23/22:00
* DayNumber 2
* Hard 2
* Level 4
* Score 1443
*/
public class Num763 {
public List<Integer> partitionLabels(String s) {
int[] last = new int[26];
for (int i = 0; i < s.length(); i++) {
last[s.charAt(i) - 'a'] = i;
}
int end=0;
int start=0;
List<Integer> res=new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
end=Math.max(end,last[s.charAt(i)-'a']);
if(end==i){
res.add(end-start+1);
start=i+1;
}
}
return res;
}
}