码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode - 253. Meeting Rooms II


    Description

    Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

    Example 1:

    Input: intervals = [[0,30],[5,10],[15,20]]
    Output: 2
    
    • 1
    • 2

    Example 2:

    Input: intervals = [[7,10],[2,4]]
    Output: 1
    
    • 1
    • 2

    Constraints:

    1 <= intervals.length <= 10^4
    0 <= starti < endi <= 10^6
    
    • 1
    • 2

    Solution

    Heap

    Solved after hints…

    Think about how we would approach this problem in a very simplistic way. We will allocate rooms to meetings that occur earlier in the day v/s the ones that occur later on, right?

    If you’ve figured out that we have to sort the meetings by their start time, the next thing to think about is how do we do the allocation?
    There are two scenarios possible here for any meeting. Either there is no meeting room available and a new one has to be allocated, or a meeting room has freed up and this meeting can take place there.

    So use a min-heap to store the ending time, every time we visit a new interval, compare the start time with the earliest ending time. If the start time begins later than the earliest ending time, then we could free up the room and allocate the room to the new interval. Otherwise we need to assign a new room for the new interval.

    Time complexity: o ( n log ⁡ n ) o(n\log n ) o(nlogn)
    Space complexity: o ( n ) o(n) o(n)

    Sort + sweep

    For all start, +1 at the point, and -1 for all ending points. Then sweep through all the points.

    Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    Heap

    class Solution:
        def minMeetingRooms(self, intervals: List[List[int]]) -> int:
            heap = []
            intervals.sort(key=lambda x: x[0])
            res = 0
            for i in range(len(intervals)):
                if heap and heap[0] <= intervals[i][0]:
                    heapq.heappop(heap)
                heapq.heappush(heap, intervals[i][1])
                res = max(res, len(heap))
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Sort + sweep

    class Solution:
        def minMeetingRooms(self, intervals: List[List[int]]) -> int:
            meetings = {}
            for start, end in intervals:
                meetings[start] = meetings.get(start, 0) + 1
                meetings[end] = meetings.get(end, 0) - 1
            points = sorted(meetings)
            res = 0
            room = 0
            for each_point in points:
                room += meetings[each_point]
                res = max(res, room)
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    2018年java进阶需要关注的公众号
    AWS EC2入门指南中创建和配置云虚拟机实例的基本步骤
    (附源码)ssm捐赠救助系统 毕业设计 060945
    ES6 类
    Kotlin - 改良责任链模式
    Go语言RPC开发深度指南:net/rpc包的实战技巧和优化策略
    Node.js中常用的设计模式有哪些?
    vue + element UI【实战】打字闯关(含按键监听、按键音效、字符匹配、动态样式、结果判定、数据统计、音效获取和剪辑等实用技巧)
    2023-10-28 思考-精力管理-分析
    0817(033天 线程/进程04 线程安全)
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/133782282
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号