252. Meeting Rooms - Easy

...

 

M1: time = O(nlogn), space = O(logn) for sorting

class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        for(int i = 0; i + 1 < intervals.length; i++) {
            if(intervals[i + 1][0] < intervals[i][1]) {
                return false;
            }
        }
        return true;
    }
}

 

M2: define an interval

/**
 * 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) {
        Arrays.sort(intervals, (a, b) -> a.start - b.start);
        for(int i = 0; i + 1< intervals.length; i++) {
            if(intervals[i+1].start < intervals[i].end)
                return false;
        }
        return true;
    }
}

 

posted @ 2020-11-11 16:52  fatttcat  阅读(80)  评论(0编辑  收藏  举报