力扣2402.会议室 III

力扣2402.会议室 III

  • 双堆模拟

    • 一个堆存未占用的会议室编号
    • 一个堆存已占用的结束时间和编号
cpp 复制代码
  class Solution {
  public:
      int mostBooked(int n, vector<vector<int>>& meetings) {
          int cnt[n];
          memset(cnt,0,sizeof(cnt));
          priority_queue<int,vector<int>,greater<>> idle;
          for(int i=0;i<n;i++) idle.push(i);
          priority_queue<pair<long,int>,vector<pair<long,int>>,greater<>> using_;
          //按照开始时间排序
          sort(meetings.begin(),meetings.end(),[](auto &a,auto &b){
              return a[0] <b[0];
          });
          for(auto &m:meetings)
          {
              long st = m[0],end = m[1],id;
              //已经到了结束时间,弹出去
              while(!using_.empty() && using_.top().first <= st)
              {
                  idle.push(using_.top().second);
                  using_.pop();
              }
              //没有空会议室了
              if(idle.empty())
              {
                  auto [e,i] = using_.top();
                  using_.pop();
                  //结束时间 + 等待时间
                  end += e - st;
                  id = i;
              }
              //有空会议室
              else
              {
                  id = idle.top();
                  idle.pop();
              }
              cnt[id] ++;
              using_.emplace(end,id);
          }
          int ans = 0;
          //取最大的cnt
          for(int i=0;i<n;i++)
              if(cnt[i] > cnt[ans])
                  ans = i;
          return ans;
      }
  };
相关推荐
泯仲3 分钟前
Ragent项目7种设计模式深度解析:从源码看设计模式落地实践
java·算法·设计模式·agent
dulu~dulu7 分钟前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
moonsea020317 分钟前
【无标题】
算法
佑白雪乐38 分钟前
<ACM进度212题>[2026-3-1,2026-3-26]
算法·leetcode
穿条秋裤到处跑42 分钟前
每日一道leetcode(2026.03.26):等和矩阵分割 II
算法·leetcode·矩阵
平凡灵感码头1 小时前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
哔哔龙1 小时前
Android OpenCV 实战:图片轮廓提取与重叠轮廓合并处理
android·算法
hz_zhangrl1 小时前
CCF-GESP 等级考试 2026年3月认证C++三级真题解析
c++·算法·程序设计·gesp·gesp2026年3月·gesp c++三级
x_xbx1 小时前
LeetCode:1. 两数之和
数据结构·算法·leetcode
x_xbx1 小时前
LeetCode:49. 字母异位词分组
算法·leetcode·职场和发展