[JAVA] Remove all conflicts with iteration & min_end / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        public int eraseOverlapIntervals(Interval[] intervals) {
            if(intervals.length == 0)
                return 0;
            
            Arrays.sort(intervals, (i1, i2) -> {
               if(i1.start == i2.start)
                   return i1.end - i2.end;
               return i1.start - i2.start; 
            });
            
            int count = 0;
            int end = intervals[0].end;
            for(int i = 1; i < intervals.length; i++){
                if(intervals[i].start < end){ 
                    end = Math.min(intervals[i].end, end);
                    count++;
                }
                else{
                    end = Math.max(intervals[i].end, end);
                }
            }
            
            return count;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.