1. 程式人生 > >911. Online Election(python+cpp)(速度略慢)

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

題目:

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裡面,都需要–,具體還是要看函式的實際作用。
速度略慢啊啊