力扣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];
      }
  };
相关推荐
前端小L20 小时前
回溯算法专题(八):精细化切割——还原合法的「IP 地址」
数据结构·算法
Hcoco_me1 天前
大模型面试题17:PCA算法详解及入门实操
算法
跨境卫士苏苏1 天前
亚马逊AI广告革命:告别“猜心”,迎接“共创”时代
大数据·人工智能·算法·亚马逊·防关联
云雾J视界1 天前
当算法试图解决一切:技术解决方案主义的诱惑与陷阱
算法·google·bert·transformer·attention·算法治理
Xの哲學1 天前
Linux Miscdevice深度剖析:从原理到实战的完整指南
linux·服务器·算法·架构·边缘计算
夏乌_Wx1 天前
练题100天——DAY23:存在重复元素Ⅰ Ⅱ+两数之和
数据结构·算法·leetcode
立志成为大牛的小牛1 天前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法
沿着路走到底1 天前
将数组倒序,不能采用reverse,算法复杂度最低
算法
IDIOT___IDIOT1 天前
KNN and K-means 监督与非监督学习
学习·算法·kmeans