Sunday, December 6, 2015

[leetcode]Meeting Room II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals == null || intervals.length == 0){
            return 0;
        }
        PriorityQueue<int[]> queue = new PriorityQueue<int[]>(1, new Comparator<int[]>(){
           public int compare(int[] o1, int[] o2){
               if (o1[0] == o2[0]) return o1[1]-o2[1];
               return o1[0]-o2[0];
           } 
        });
        
        
        for (int i = 0; i < intervals.length; i++){
            queue.add(new int[]{intervals[i].start, 1});
            queue.add(new int[]{intervals[i].end, -1});
        }
        int maxSameTime = 0;
        int count = 0;
        while (!queue.isEmpty()){
            int[] time = queue.poll();
            count += time[1];
            maxSameTime = Math.max(maxSameTime, count);
        }
        return maxSameTime;
    }
}

No comments:

Post a Comment