Meeting Rooms

题目
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

For example,

Given [[0, 30],[5, 10],[15, 20]],
return false.

答案

class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        List interval_list = Arrays.asList(intervals);
        // sort by start time
        Collections.sort(interval_list, new Comparator() {
            public int compare(Interval c1, Interval c2) {
                if (c1.start > c2.start) return 1;
                if (c1.start < c2.start) return -1;
                return 0;
            }
        });

        for(int i = 0; i < interval_list.size() - 1; i++) {
            Interval i1 = interval_list.get(i);
            Interval i2 = interval_list.get(i + 1);
            if(i1.end > i2.start) return false;
        }
        return true;
    }

你可能感兴趣的:(Meeting Rooms)