力扣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;
      }
  };
相关推荐
SunnyKriSmile1 小时前
C语言译码操作
c语言·算法·if语句·译码操作·switch语句
小小小CTFER1 小时前
理论题] 2025 年 “技耀泉城” 海右技能人才大赛网络安全知识竞赛题目(二)
算法·安全·web安全
断剑zou天涯3 小时前
【算法笔记】暴力递归尝试
java·笔记·算法
油泼辣子多加4 小时前
【实战】自然语言处理--长文本分类(1)DPCNN算法
算法·自然语言处理·分类
I'm a winner4 小时前
基于YOLO算法的医疗应用专题:第一章 计算机视觉与深度学习概述
算法·yolo·计算机视觉
vir025 小时前
P1928 外星密码(dfs)
java·数据结构·算法·深度优先·1024程序员节
喜欢吃燃面5 小时前
数据结构算法题:list
开发语言·c++·学习·算法·1024程序员节
胡萝卜3.05 小时前
C++ list核心接口与实战技巧
数据结构·c++·list·list使用
寂静山林5 小时前
UVa 12991 Game Rooms
算法·1024程序员节