LeetCode 253 - Meeting Rooms II
http://sbzhouhao.net/LeetCode/LeetCode-Meeting-Rooms.html
https://discuss.leetcode.com/topic/20959/ac-clean-java-solution
http://leetcode.tgic.me/meeting-rooms/index.html
https://leetcode.com/discuss/73547/easy-java-solution-beat-98%25
https://leetcode.com/discuss/56676/java-o-n-k-solution-two-passes-k-start-end-in-interval
Java O(n*k) solution, two passes, k=start -end in Interval
public boolean canAttendMeetings(Interval[] intervals) { int start=Integer.MAX_VALUE; int end=Integer.MIN_VALUE; int s=0; int e=0; for(Interval i:intervals){ start=Math.min(i.start,start); end=Math.max(i.end,end); } boolean[] result=new boolean[end-start+1]; for(Interval i:intervals){ s=i.start; e=i.end; for(int j=s;j<e;j++){ if(result[j-start]==true )return false; result[j-start]=true; } } return true; }
LIKE CODING: LeetCode [252] Meeting Rooms
http://leetcode.tgic.me/meeting-rooms-ii/index.html
http://buttercola.blogspot.com/2014/11/facebook-maximum-number-of-overlapping.html
http://sbzhouhao.net/LeetCode/LeetCode-Meeting-Rooms.html
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.public boolean canAttendMeetings(Interval[] intervals) { assert intervals != null : "null input"; Arrays.sort(intervals, (o1, o2) -> { int r = o1.start - o2.start; return r == 0 ? o1.end - o2.end : r; }); for (int i = 1; i < intervals.length; i++) { Interval i1 = intervals[i - 1]; Interval i2 = intervals[i]; if (i1.end > i2.start) { return false; } } return true; }http://www.programcreek.com/2014/07/leetcode-meeting-rooms-java/
public boolean canAttendMeetings(Interval[] intervals) { Arrays.sort(intervals, new Comparator<Interval>(){ public int compare(Interval a, Interval b){ return a.start-b.start; } }); for(int i=0; i<intervals.length-1; i++){ if(intervals[i].end>intervals[i+1].start){ return false; } } return true; } |
https://discuss.leetcode.com/topic/20959/ac-clean-java-solution
http://leetcode.tgic.me/meeting-rooms/index.html
11 public boolean canAttendMeetings(Interval[] intervals) {
12 Arrays.sort(intervals, (a, b) -> a.start - b.start);
13
14 int maxend = 0;
15
16 for(Interval i : intervals){
17 if(i.start < maxend){
18 return false;
19 }
20
21 maxend = Math.max(maxend, i.end);
22 }
23
24 return true;
25 }
http://segmentfault.com/a/1190000003894670 public boolean canAttendMeetings(Interval[] intervals) {
if(intervals == null || intervals.length == 0) return true;
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval i1, Interval i2){
return i1.start - i2.start;
}
});
int end = intervals[0].end;
// 检查每一个Interval
for(int i = 1; i < intervals.length; i++){
// 如果Interval的开始时间小于之前最晚的结束时间,就返回假
if(intervals[i].start < end) return false;
end = Math.max(end, intervals[i].end);
}
return true;
}
https://leetcode.com/discuss/50912/ac-clean-java-solution
In the above implementation, you use the sort method to get all objects sorted - O(nlogn) time complexity. After sorting you go through each object comparing it with the previous object to find if there is an overlap - O(n) time complexity.
What if you can find if there is an overlap while you sort and raise a flag?
Here's my implementation:
private boolean canAttendMeetings(Interval[] intervals) {
try {
Arrays.sort(intervals, new IntervalComparator());
} catch (Exception e) {
return false;
}
return true;
}
private class IntervalComparator implements Comparator<Interval> {
@Override
public int compare(Interval o1, Interval o2) {
if (o1.start < o2.start && o1.end <= o2.start)
return -1;
else if (o1.start > o2.start && o1.start >= o2.end)
return 1;
throw new RuntimeException();
}
}
While comparing two objects, if there is an overlap, it throws an Exception. The exception is caught during the run of the sort method is called. This avoids the extra search you require to go through the whole array to look for an overlap, if you don't mind making the comparison a little complex than the simple:
o1.start - o2.start
Interesting idea, though you might be replacing a cost of c1·n by a cost of c2·n·log(n)...
Sorting and linear search: c1.nlogn + c2.n
Sorting with throw error : c3.nlogn
c1 < c3
c2 < c3
?
public boolean canAttendMeetings(Interval[] intervals) {
int len=intervals.length;
if(len==0){
return true;
}
int[]begin=new int[len];
int[]stop=new int[len];
for(int i=0;i<len;i++){
begin[i]=intervals[i].start;
stop[i]=intervals[i].end;
}
Arrays.sort(begin);
Arrays.sort(stop);
int endT=0;
for(int i=1;i<len;i++){
if(begin[i]<stop[i-1]){
return false;
}
}
return true;
}
Java O(n*k) solution, two passes, k=start -end in Interval
public boolean canAttendMeetings(Interval[] intervals) { int start=Integer.MAX_VALUE; int end=Integer.MIN_VALUE; int s=0; int e=0; for(Interval i:intervals){ start=Math.min(i.start,start); end=Math.max(i.end,end); } boolean[] result=new boolean[end-start+1]; for(Interval i:intervals){ s=i.start; e=i.end; for(int j=s;j<e;j++){ if(result[j-start]==true )return false; result[j-start]=true; } } return true; }
LIKE CODING: LeetCode [252] Meeting Rooms
bool canAttendMeetings(vector<Interval>& intervals) {
int n = intervals.size();
sort(intervals.begin(), intervals.end(), compareByStart);
int last_end = 0;
for
(int i=0; i<n; ++i){
if
(intervals[i].start<last_end)
return
false
;
last_end = max(last_end, intervals[i].end);
}
return
true
;
}
http://buttercola.blogspot.com/2014/11/facebook-maximum-number-of-overlapping.html
public
int
numOverLaps(List<Integer> start, List<Integer> end) {
if
(start ==
null
|| start.size() ==
0
|| end ==
null
|| end.size() ==
0
) {
return
0
;
}
if
(start.size() != end.size()) {
return
0
;
}
Collections.sort(start);
Collections.sort(end);
int
startP =
0
;
int
endP =
0
;
int
numActive =
0
;
int
numOverlap =
0
;
while
(startP < start.size() && endP < end.size()) {
if
(start.get(startP) < end.get(endP)) {
numActive++;
numOverlap = Math.max(numOverlap, numActive);
startP++;
}
else
{
numActive--;
endP++;
}
}
return
numOverlap;
}