力扣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;
      }
  };
相关推荐
一叶知秋061 小时前
数据结构-什么是队列?
数据结构·队列
Jasmine_llq1 小时前
《CF280C Game on Tree》
数据结构·算法·邻接表·深度优先搜索(dfs)·树的遍历 + 线性累加统计
小棠师姐2 小时前
支持向量机(SVM)入门:超平面与核函数的通俗解释
算法·python机器学习·支持向量机svm·超平面可视化·核函数应用
zhongvv2 小时前
对单片机C语言指针的一些理解
c语言·数据结构·单片机·指针·汇编语言
im_AMBER2 小时前
Leetcode 102 反转链表
数据结构·c++·学习·算法·leetcode·链表
今儿敲了吗2 小时前
01|多项式输出
c++·笔记·算法
Xの哲學3 小时前
深入剖析Linux文件系统数据结构实现机制
linux·运维·网络·数据结构·算法
AlenTech3 小时前
200. 岛屿数量 - 力扣(LeetCode)
算法·leetcode·职场和发展
C雨后彩虹3 小时前
竖直四子棋
java·数据结构·算法·华为·面试
不如自挂东南吱4 小时前
空间相关性 和 怎么捕捉空间相关性
人工智能·深度学习·算法·机器学习·时序数据库