首页 > 其他 > 详细

LeetCode-Meeting Rooms II

时间:2016-09-13 06:45:34      阅读:233      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/lishiblog/p/5867062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!