*LeetCode-Meeting Rooms II

bsbcarter發表於2015-11-24

首先interval陣列按照start 排序 然後建立一個以end排序的priority queue 這樣每次按照start的先後順序入隊,入隊之前要把所有在這個start之前都結束的會議poll出來

每次更新room數量 

public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        Arrays.sort ( intervals, new Comparator<Interval>(){
            public int compare ( Interval int1, Interval int2 ){
                return int1.start - int2.start;
            }
        });
        PriorityQueue <Interval> que = new PriorityQueue<Interval>( new Comparator<Interval>(){
            public int compare ( Interval int1, Interval int2 ){
                return int1.end - int2.end;
            }
        });
        int rooms = 0;
        for ( int i = 0; i < intervals.length; i ++ ){
            while ( !que.isEmpty() && que.peek().end <= intervals[ i ].start ){
                que.poll();
            }
            que.offer ( intervals[ i ] );
            rooms = Math.max ( rooms, que.size() );
        }
        return rooms;
    }
}


相關文章