LeetCode-Meeting Rooms II

Posted LiBlog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Meeting Rooms II相关的知识,希望对你有一定的参考价值。

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;
    }
}

 

以上是关于LeetCode-Meeting Rooms II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Meeting Rooms II

LeetCode 253. Meeting Rooms II(会议室)

253. Meeting Rooms II

253. Meeting Rooms II

253. Meeting Rooms II

253.Meeting Rooms II