[LeetCode 252] Meeting Rooms

Mavs發表於2015-10-12

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<Interval>() {
            @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;i<intervals.length;i++) {
            Interval t1 = intervals[i-1];
            Interval t2 = intervals[i];
            if(t1.end>t2.start) return false;
        }
        return true;
    }


相關文章