1288_RemoveCoveredIntervals - a920604a/leetcode GitHub Wiki


title: 1288. Remove Covered Intervals tags:
- sorting categories: leetcode comments: false

solution

class Solution {
public:
    int removeCoveredIntervals(vector<vector<int>>& intervals) {
        // ๆŽ’ๅบ๏ผŒๆŒ‰่ตท้ปžๅ‡ๅบ๏ผŒ่ตท้ปž็›ธๅŒๆกˆ็ต‚้ปž้™ๅบ
        
        sort(intervals.begin(), intervals.end(),[](vector<int>& a, vector<int>&b){
            if(a[0] == b[0] ) return a[1]>b[1];
            return a[0]<b[0];
        });
        int n = intervals.size(), start = intervals[0][0], end = intervals[0][1];
        int remove = 0;
        for(int i=1;i<n;++i){
            vector<int> & cur = intervals[i];
            // [1,4] [3,5] ๅ€้–“้‡็–Š => ๅฐ‡end ๅ‘ๅณๆ“ดๅผต
            if( end >= cur[0] && end<cur[1]){
                end = cur[1];
            }
            // [1,4] [2,3] ๅฎŒๅ…จ่ฆ†่“‹ => ๅฏไปฅ่ฆ†่“‹ไธ€ๅ€‹ๅ€้–“ใ€‚
            else if(end>= cur[0] && end>=cur[1]){
                remove++;
            }
            // [1,2] [4,6] ไธ้‡็–Š
            else{
                start = cur[0];
                end = cur[1];
            }
        }
        return n - remove;
        
    }
};

analysis

  • time complexity O(nlogn)
  • space complexity O(1)
โš ๏ธ **GitHub.com Fallback** โš ๏ธ