Created
July 22, 2017 04:08
-
-
Save jianminchen/e3133e229461e005ef62286602a23cd9 to your computer and use it in GitHub Desktop.
Meeting Room II - study Java Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int minMeetingRooms(Interval[] intervals) { | |
if(intervals == null || intervals.length == 0) return 0; | |
Arrays.sort(intervals, new Comparator<Interval>(){ | |
public int compare(Interval i1, Interval i2){ | |
return i1.start - i2.start; | |
} | |
}); | |
// 用堆来管理房间的结束时间 | |
PriorityQueue<Integer> endTimes = new PriorityQueue<Integer>(); | |
endTimes.offer(intervals[0].end); | |
for(int i = 1; i < intervals.length; i++){ | |
// 如果当前时间段的开始时间大于最早结束的时间,则可以更新这个最早的结束时间为当前时间段的结束时间,如果小于的话,就加入一个新的结束时间,表示新的房间 | |
if(intervals[i].start >= endTimes.peek()){ | |
endTimes.poll(); | |
} | |
endTimes.offer(intervals[i].end); | |
} | |
// 有多少结束时间就有多少房间 | |
return endTimes.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment