452. 用最少数量的箭引爆气球 - 力扣(LeetCode)
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length <= 1) {
return points.length;
}
Arrays.sort(points, (o1, o2) -> {
if (o1[0] != o2[0]) {
return o1[0] < o2[0] ? -1 : 1;
} else {
return o2[1] < o1[1] ? -1 : 1;
}
});
int result = 0;
for (int i = 0; i < points.length - 1; i++) {
// 当前气球有覆盖下一个气球
if (points[i][1] >= points[i + 1][0]) {
if(points[i][1]<points[i + 1][1]){
points[i + 1][1] = points[i][1];
}
} else {
result++;
}
}
return result + 1;
}
}
763. 划分字母区间 - 力扣(LeetCode)
class Solution {
public List<Integer> partitionLabels(String s) {
char[] chars = s.toCharArray();
HashMap<Character,Integer> map = new HashMap<>();
for(int i=0;i<chars.length;i++){
map.put(chars[i],i);
}
List<Integer> result = new ArrayList<>();
for(int i=0;i<chars.length;i++){
int maxRemote = map.get(chars[i]);
for(int j=i;j<=maxRemote;j++){
maxRemote = Math.max(maxRemote,map.get(chars[j]));
}
result.add(maxRemote-i+1);
i = maxRemote;
}
return result;
}
}
435. 无重叠区间 - 力扣(LeetCode)
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if(intervals.length<=1){
return 0;
}
Arrays.sort(intervals, (o1, o2) -> {
if(o1[0] != o2[0]){
return o1[0] - o2[0];
}else{
return o2[1] - o1[1];
}
});
int result = 1;
for(int i=1;i<intervals.length;i++){
if(intervals[i][0] >= intervals[i-1][1]){
result++;
continue;
}
intervals[i][1] = Math.min(intervals[i][1],intervals[i-1][1]);
}
return intervals.length - result ;
}
}