My code:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if (intervals == null || intervals.length == 0)
return 0;
int[] starter = new int[intervals.length];
int[] ender = new int[intervals.length];
for (int i = 0; i < intervals.length; i++) {
starter[i] = intervals[i].start;
ender[i] = intervals[i].end;
}=
Arrays.sort(starter);
Arrays.sort(ender);
int endPoint = 0;
int counter = 0;
for (int i = 0; i < intervals.length; i++) {
if (starter[i] < ender[endPoint]) {
counter++;
}
else {
endPoint++;
}
}
return counter;
}
}
这个方法比较巧,直接看的答案。
解释下,为什么 start[i] >= end[ep] 时,为什么endPointer++, i 也需要 ++
因为这个时候,start > end,
那么,时间上是独立的,不需要新开一间房。于是,start跟end都往后移动一格,这个时候再开始算,如果有重复的,再给room加上去。
参考网页:
https://leetcode.com/discuss/82292/explanation-super-easy-java-solution-beats-from-%40pinkfloyda
还有一个 O(n ^ 2)的做法,
参考网页是:
https://leetcode.com/discuss/86333/easy-and-concise-java-sol
Anyway, Good luck, Richardo!
My code:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if (intervals == null || intervals.length == 0) {
return 0;
}
int[] start = new int[intervals.length];
int[] end = new int[intervals.length];
for (int i = 0; i < intervals.length; i++) {
start[i] = intervals[i].start;
end[i] = intervals[i].end;
}
Arrays.sort(start);
Arrays.sort(end);
int counter = 0;
int endLocation = 0;
for (int i = 0; i < start.length; i++) {
if (start[i] < end[endLocation]) {
counter++;
}
else {
endLocation++;
}
}
return counter;
}
}
还是看了以前的答案做出来的。很巧妙。
先把start, end 都排序。
然后如果start >= end,那么说明,start这一个会议,可以和end那一组会议公用一个会议室。然后,这个会议室的结束时间就不再是现在的end了。而要往后延。
如果 start < end, 那么必须开一个会议室,counter++
Anyway, Good luck, Richardo! -- 09/15/2016