[LeetCode 252] 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.

Solution:

sort the interval based on the start time, if have same start time use end time.

go through interval to see if there any overlaps.

    class Interval {
        int start;
        int end;
        public Interval(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
    public boolean canAttendMeetings(Interval[] intervals) {
        Arrays.sort(intervals, new Comparator() {
            @Override
            public int compare(Interval o1, Interval o2) {
                int r = o1.start - o2.start;
                return r==0? o1.end - o2.end : r;
            }
        });
        for(int i=1;it2.start) return false;
        }
        return true;
    }


你可能感兴趣的:(Java,Leetcode)