Java solution for O(nlgn), similar to merge interval


  • -1
    B

    The idea is simple. We maintain a list of rooms of end time. For every new interval we loop through , if the start time of this interval doesn't overlap one of the end time in the list, we don't need a new room for this interval.

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

    }


Log in to reply
 

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