simple java solution with super detailed explanation


  • 0
    2

    This is actually Activity selection problem, refer to this site
    https://en.wikipedia.org/wiki/Activity_selection_problem

    public class Solution {
        public int eraseOverlapIntervals(Interval[] intervals) {
            Arrays.sort(intervals, new C());
            long last = Long.MIN_VALUE;
            int count = intervals.length;
            for (Interval i : intervals) {
                if (last <= i.start) {
                    last = i.end;
                    count--;
                }
            }
            return count;
        }
        
        class C implements Comparator<Interval> {
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.end, i2.end);
            }
        }
    }
    

Log in to reply
 

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