Meeting Rooms


  • 0

    Click here to see the full article post


  • 0
    P

    I believe if you sort by end time then it'll also work for this problem.


  • 0
    R

    Yeah sorting by end time will also work.


  • 0
    P
    /**
     * Definition for an interval.
     * public class Interval {
     *     int start;
     *     int end;
     *     Interval() { start = 0; end = 0; }
     *     Interval(int s, int e) { start = s; end = e; }
     * }
     */
    class Solution {
    	public boolean canAttendMeetings(Interval[] intervals) {
    
    		if (intervals.length == 0 || intervals.length == 1)
    			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].start >= intervals[i - 1].end) {
    				continue;
    			}
    			return false;
    		}
    		return true;
    	}
    }

Log in to reply
 

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