911. Online Election(python+cpp)(速度略慢)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21275321/article/details/83988268

题目:

In an election, the i-th vote was cast for persons[i] at time times[i].
Now, we would like to implement the following query function:
TopVotedCandidate.q(int t) will return the number of the person that was leading the election at time t.
Votes cast at time t will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins.
Example 1:

Input: ["TopVotedCandidate","q","q","q","q","q","q"], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]
Output: [null,0,1,1,0,0,1] 
Explanation:  At time 3, the votes are [0], and 0 is leading. At time 12, the votes are 
[0,1,1], and 1 is leading.At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties 
go to the most recent vote.) This continues for 3 more queries at time 15, 24, and 8.  

Note:
1 <= persons.length = times.length <= 5000
0 <= persons[i] <= persons.length
times is a strictly increasing array with all elements in [0, 109].
TopVotedCandidate.q is called at most 10000 times per test case.
TopVotedCandidate.q(int t) is always called with t>=times[0].

解释:
python代码:

class TopVotedCandidate:

    def __init__(self, persons, times):
        """
        :type persons: List[int]
        :type times: List[int]
        """
        self.times=times
        count={}
        self.leads=[]
        lead=-1
        for i in range(len(persons)):
            p,t=persons[i],times[i]
            count[p]=count.get(p,0)+1
            if count[p]>=count.get(lead,-1):
                lead=p
            self.leads.append(lead)

    def q(self, t):
        """
        :type t: int
        :rtype: int
        """
        return self.leads[bisect.bisect(self.times,t)-1]

# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)

c++为什么超时?c++用find()速度比count()快。

#include <map>
using namespace std;
class TopVotedCandidate {
public:
    vector<int>global_times;
    vector<int> leads;
    TopVotedCandidate(vector<int> persons, vector<int> times) {
        global_times=times;
        int lead=-1;
        map<int,int>_count;
        for (int i=0;i<persons.size();i++)
        {
            int p=persons[i];
            int t=times[i];
            _count[p]+=1;
            if (_count[p]>=_count[lead])
                lead=p;
            leads.push_back(lead);  
        }
        
    }
    
    int q(int t) {
        int idx=lower_bound(global_times.begin(),global_times.end(),t)-global_times.begin();
        if (find(global_times.begin(),global_times.end(),t)!=global_times.end())
            idx+=1;
        return leads[idx-1];
    }
};

/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj.q(t);
 */

总结:
python
bisect.bisect_left(a,x, lo=0, hi=len(a)) :
查找在有序列表 a 中插入 x 的index。lo 和 hi 用于指定列表的区间,默认是使用整个列表。如果 x 已经存在,在其左边插入。返回值为 index。
bisect.bisect_right(a,x, lo=0, hi=len(a))
bisect.bisect(a, x,lo=0, hi=len(a)) :
这2个函数和 bisect_left 类似,但如果 x 已经存在,在其右边插入
注意,c++的lower_bound和python的bisect.bisect()是不一样的,bisect.bisect()是返回右侧的位置,lower_bound返回一个非递减序列[first, last)中的第一个大于等于值val的位置,所以使用lower_bound,如果t不在times里面,需要–,使用bisect.bisect()不管t在不在times里面,都需要–,具体还是要看函数的实际作用。
速度略慢啊啊

猜你喜欢

转载自blog.csdn.net/qq_21275321/article/details/83988268