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.

 

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     public int start;
 5  *     public int end;
 6  *     public Interval() { start = 0; end = 0; }
 7  *     public Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public class IntervalComparer : IComparer<Interval>
12     {
13         public int Compare(Interval a, Interval b)
14         {
15             return a.start == b.start ? a.end - b.end : a.start - b.start;
16         }
17     }
18     
19     
20     public bool CanAttendMeetings(Interval[] intervals) {
21         if (intervals == null || intervals.Length < 2) return true;
22         
23         Array.Sort(intervals, new IntervalComparer());
24         
25         int cur = intervals[0].end;
26         
27         for (int i = 1; i < intervals.Length; i++)
28         {
29             if (intervals[i].start < cur) return false;
30             cur = intervals[i].end;
31         }
32         
33         return true;
34     }
35 }

 

posted @ 2017-12-19 03:06  逸朵  阅读(144)  评论(0)    收藏  举报