Accepted Java Solution Using Sort


  • 0
    public class Solution {
        public boolean canAttendMeetings(Interval[] intervals) {
            if(intervals.length <= 1) return true;
            Arrays.sort(intervals, new Comparator<Interval>(){
                public int compare(Interval o1, Interval o2){
                    //ascending order
                    return o1.start - o2.start;
                }
            });
            //Return false if next meetings start time is less than previous meetings end time
            for(int i=0; i < intervals.length-1; i++){
                if(intervals[i].end > intervals[i+1].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.