LeetCode-Meeting Rooms II

LiBlog發表於2016-09-13

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.

Analysis:

It is the same with LintCode-Max number of airplanes.

Solution:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals.length<2) return intervals.length;
        
        HashMap<Integer,Integer> timeCounts = new HashMap<Integer,Integer>();
        for (Interval conf : intervals){
            int startCount = timeCounts.getOrDefault(conf.start,0)+1;
            timeCounts.put(conf.start,startCount);
            int endCount = timeCounts.getOrDefault(conf.end,0)-1;
            timeCounts.put(conf.end,endCount);
        }
        
        List<Integer> times = new ArrayList<Integer>();
        times.addAll(timeCounts.keySet());
        Collections.sort(times);
        
        int maxNum = 0, curNum = 0;
        for (int time : times){
            int count = timeCounts.get(time);
            curNum += count;
            maxNum = Math.max(maxNum,curNum);
        }
        return maxNum;
    }
}

 

相關文章