Leetcode: 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.

Implement a Comparator

Syntax: don't forget the public sign when defining a function

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public boolean canAttendMeetings(Interval[] intervals) {
12         if (intervals==null || intervals.length==0 || intervals.length==1) return true;
13         Comparator comp = new Comparator() {
14             public int compare(Interval i1, Interval i2) {
15                 return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start;
16             }
17         };
18         
19         Arrays.sort(intervals, comp);
20         Interval pre = intervals[0];
21         for (int i=1; i) {
22             Interval cur = intervals[i];
23             if (cur.start < pre.end) return false;
24             pre = cur;
25         }
26         return true;
27     }
28 }

 

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