Java sort solution beats 87%


  • 0
    M
    public class Solution {
        public int eraseOverlapIntervals(Interval[] A) {
            if(A == null || A.length == 0) return 0;
            Arrays.sort(A, new Comparator<Interval> () {
               public int compare(Interval a, Interval b) {
                   return Integer.compare(a.start, b.start);
               } 
            });
            int rst = 0;
            Interval tail = A[0];
            for(int i = 1; i < A.length; ++i) {
                if(A[i].start >= tail.end) tail = A[i];
                else {
                    rst++;
                    if(A[i].end < tail.end) tail = A[i];
                }
            }
            return rst;
            
        }
    }
    

Log in to reply
 

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