rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

435. 无重叠区间 #110

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago
class Solution {
     public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> {
            if (a[0] == b[0]) return a[1] - b[1];
            return a[0] - b[0];
        });
        int ans = 0;
        for (int i = 0; i < intervals.length - 1; i++){
            if (intervals[i][0] == intervals[i + 1][0]){
                ans++;
                intervals[i + 1][1] = Math.min(intervals[i][1], intervals[i+1][1]);
                continue;
            }
            // 重叠
            if (intervals[i + 1][0] < intervals[i][1]){
                ans++;
                intervals[i + 1][1] = Math.min(intervals[i][1], intervals[i+1][1]);
            }
        }
        return ans;
    }
}