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
.
Problem Solving Ideas:
First to start sort, O (NLGN), then compare si+1 >= Ei, if true, continue compare, if False, return false. Total Complexity:o (NLGN)
Issue: Note the arrays.sort in Java (intervals, new comparator<interval> () {...}), how to use
Java Code:
/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Public Booleancanattendmeetings (interval[] intervals) {if(Intervals.length <= 1) { return true; } arrays.sort (Intervals,NewComparator<interval>(){ Public intCompare (Interval A, Interval b) {returnA.start-B.start; } }); for(inti = 1; i < intervals.length; i++) { if(Intervals[i].start < Intervals[i-1].end) {return false; } } return true; }}
Reference:
1. Https://leetcode.com/discuss/50912/ac-clean-java-solution
Leetcode Meeting Rooms