Java concise code, greedy method


  • 0
    public class Solution {
        public int eraseOverlapIntervals(Interval[] intervals) {
            if(intervals.length < 1){
                return 0;
            }
            
            Arrays.sort(intervals, ((a,b) -> (a.end - b.end)));
            int sol = 0;
            int firstEnd = intervals[0].start;
            for(Interval interval : intervals){
                if(firstEnd <= interval.start){
                    firstEnd = interval.end;
                }
                else{
                    sol++;
                }
            }
            return sol;
        }
    }
    

Log in to reply
 

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