Java clean solution, using Arrays.sort


  • 0
    M

    Time: O(nlogn)

        public boolean canAttendMeetings(Interval[] intervals) {
            if(intervals==null || intervals.length<2) return true;
            Arrays.sort(intervals, new Comparator<Interval>(){
                public int compare(Interval i1, Interval i2){
                    return i1.start - i2.start;
                }
            });
            for(int i=1; i<intervals.length; i++){
                if(intervals[i-1].end > intervals[i].start)
                    return false;
            }
            return true;
        }
    

Log in to reply
 

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