Simple Java solution - beats 88.53%


  • 0
    V
    public class Solution {
        public int eraseOverlapIntervals(Interval[] intervals) {
            if(intervals==null || intervals.length==0)
                return 0;
            
            Arrays.sort(intervals, new Comparator<Interval>(){
                    public int compare(Interval i1, Interval i2){
                        return i1.start-i2.start;
                    }
                });
            
            int minRemove=0;
            int prevEnd=intervals[0].end;
            
            for(int i=1; i<intervals.length; i++){
                if(prevEnd>intervals[i].start){
                    minRemove++;
                    prevEnd=Math.min(prevEnd,intervals[i].end);
                }
                else{
                    prevEnd=intervals[i].end;
                }
            }
            return minRemove;
            
        }
    }

Log in to reply
 

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