力扣911.在线选举

力扣911.在线选举

  • 类似1818. 将每个时刻的候选人存储 在时间段上做二分

cpp 复制代码
  class TopVotedCandidate {
      vector<int> time,person;
  public:
      TopVotedCandidate(vector<int>& persons, vector<int>& times) {
              int n = persons.size();
              unordered_map<int,int> cnt;
              int ms = 0,mi = 0;
              time = times;
  
              for(int i=0;i<n;i++)
              {
                  if(++cnt[persons[i]] >= ms)
                  {
                      ms = cnt[persons[i]];
                      mi = persons[i];
                  }
                  person.push_back(mi);
              }
      }
      
      int q(int t) {
          int idx = ranges::upper_bound(time,t) - time.begin() - 1;
          return person[idx];
      }
  };
相关推荐
文丑颜不良啊5 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z5 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH6 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶7 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊7 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊7 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊9 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发9 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔10 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab