My simple Java solution 5ms


  • 0
    S
    public int eraseOverlapIntervals(Interval[] intervals) {
            if (intervals == null || intervals.length == 0) return 0;
    	Arrays.sort(intervals, new Comparator<Interval>() {
    		@Override
    		public int compare(Interval o1, Interval o2) {
    			return o1.start == o2.start ? o1.end-o2.end : o1.start-o2.start;
    		}
    	});
    	int res = 0;
    	int end = intervals[0].end;
    	for (int i = 1; i < intervals.length; i++) {
                    end = Math.min(intervals[i].end, end);
    		if (intervals[i].start >= end)end = intervals[i].end;
    		else res++;
    	}
    	return res;
        }
    

Log in to reply
 

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