力扣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;
      }
  };
相关推荐
雾月5523 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
Pasregret43 分钟前
访问者模式:分离数据结构与操作的设计模式
数据结构·设计模式·访问者模式
OpenC++1 小时前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
草莓啵啵~3 小时前
搜索二叉树-key的搜索模型
数据结构·c++
爱研究的小陈3 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing3 小时前
二叉树的最大宽度计算
算法·面试
丶Darling.3 小时前
26考研 | 王道 | 数据结构 | 第八章 排序
数据结构·考研·排序算法