Share my easy understand JAVA solution (beats 84%)


  • 0
    I

    public int eraseOverlapIntervals(Interval[] intervals) {

        intervals==null||intervals.length<=1) return 0;
        Arrays.sort(intervals, new Comparator<Interval>(){
            public int compare (Interval a, Interval b){
                return a.start - b.start;
            }
        });
        int count = 0;
        int start = intervals[0].start;
        int end = intervals[0].end;
        for (int i = 1; i<intervals.length;i++){
            if (intervals[i].start<end){
                start = intervals[i].start;
                end= Math.min(end, intervals[i].end);
                count++;
            }
            else {
                start = intervals[i].start;
                end = intervals[i].end;    
            }
        }
        return count;
    }

Log in to reply
 

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