栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

leetcode每日一题 911在线选举

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode每日一题 911在线选举

思路:主要是预处理,记录一个当前时刻最大值,遍历times数组,得到每个时刻的领先人,方法是记录一个当前的最大值,比较当前时刻得票人和之前领先人的得票数就行了,最后二分查找要求的时刻之前的那个投票时刻的领先人。

class TopVotedCandidate {
public:
    int ticket[5005];  //票数
    int late[5005];    //下标为i的人最近一次得票的时间。
    vector  an;
    vector  m_times;
    int mmax,nowmax;
    int mi,nowi;
    int l,l2;
    TopVotedCandidate(vector& persons, vector& times) {
        memset(ticket,0,sizeof(ticket));
        l = times.size();
        m_times = times;
        l2 = persons.size();
        mmax = nowmax = -1;
        for(int i = 0; i < l; i++){
            ticket[persons[i]]++;
            late[persons[i]] = times[i];
            nowmax = ticket[persons[i]];
            nowi = persons[i];
            if(nowmax > mmax || (nowmax == mmax && late[nowi] > late[mi])){
                mmax = nowmax;
                mi = nowi;
            }
        //    cout << mi << endl;
            an.push_back(mi);
        }
    }
    
    int q(int t) {
        if(t < m_times[0]){
            return -1;
        }else if(t > m_times[l-1]){
            return an[l-1];
        }else{
            int x = lower_bound(m_times.begin(), m_times.end(), t) - m_times.begin();
            if(m_times[x] != t){
                x--;
            }
        //    cout << t << " " << x  << endl;
            return an[x];
        }
    }
};


![](https://img-blog.csdnimg.cn/b046879bdb85477ab244b0bf0055a6e0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657018.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号