zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    观察者模式
    如何通过反射创建对象?
    java8新特性
    idea 常用快捷键--标蓝
    java多线程基础篇-01
    zookeeper单机版及操作
    redis和jedis常用api
    Mac连接服务器
    redis基本介绍及安装01
    docker 安装mobsf及部分命令01
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5867062.html
Copyright © 2011-2022 走看看