Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. // put end into heap with comparator
  2. private static int minRooms(ArrayList<Interval> intervals) {
  3. Collections.sort(intervals, new ComparactorInterval());
  4. PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
  5. int curRoom = 0;
  6. int maxRoom = 0;
  7.  
  8. for (int i = 0; i < intervals.size(); i++) {
  9. int curStart = intervals.get(i).start;
  10. int curEnd = intervals.get(i).end;
  11. while (!heap.isEmpty() && heap.peek() <= curStart) {
  12. heap.poll();
  13. curRoom--;
  14. }
  15. heap.add(curEnd);
  16. curRoom++;
  17. maxRoom = Math.max(maxRoom, curRoom);
  18. }
  19. return maxRoom;
  20. }
  21.  
  22. private static class ComparactorInterval implements Comparator<Interval> {
  23. public int compare(Interval a, Interval b) {
  24. if (a.start == b.start) {
  25. return a.end - b.end;
  26. }
  27. return a.start - b.start;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement