Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // put end into heap with comparator
- private static int minRooms(ArrayList<Interval> intervals) {
- Collections.sort(intervals, new ComparactorInterval());
- PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
- int curRoom = 0;
- int maxRoom = 0;
- for (int i = 0; i < intervals.size(); i++) {
- int curStart = intervals.get(i).start;
- int curEnd = intervals.get(i).end;
- while (!heap.isEmpty() && heap.peek() <= curStart) {
- heap.poll();
- curRoom--;
- }
- heap.add(curEnd);
- curRoom++;
- maxRoom = Math.max(maxRoom, curRoom);
- }
- return maxRoom;
- }
- private static class ComparactorInterval implements Comparator<Interval> {
- public int compare(Interval a, Interval b) {
- if (a.start == b.start) {
- return a.end - b.end;
- }
- return a.start - b.start;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement