LeetCode 911. Online Election

原题链接在这里:https://leetcode.com/problems/online-election/

题目:

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. 1 <= persons.length = times.length <= 5000
  2. 0 <= persons[i] <= persons.length
  3. times is a strictly increasing array with all elements in [0, 10^9].
  4. TopVotedCandidate.q is called at most 10000 times per test case.
  5. TopVotedCandidate.q(int t) is always called with t >= times[0].

题解:

Given a list of person vote with time, ask <= given time, who is the lead vote person.

The ask is based on time, find the floor key and get the lead vote.

We could have a structure to store mappings between time and lead vote person.

For each vote, accumulate the votes for each person and update the lead. Then update the lead person at the time.

For the given time, use binary search to find floor key and use this floor key time to get the lead person at that time.

Time Complexity: TopVotedCandidate, O(n). n = persons.length. q, O(logn).

Space: O(n).

AC Java:

 1 class TopVotedCandidate {
 2     HashMap<Integer, Integer> cachedMostVote;
 3     int [] times;
 4     
 5     public TopVotedCandidate(int[] persons, int[] times) {
 6         this.cachedMostVote = new HashMap<>();
 7         this.times = times;
 8         
 9         HashMap<Integer, Integer> count = new HashMap<>();
10         int n = persons.length;
11         int lead = -1;
12         
13         for(int i = 0; i<n; i++){
14             count.put(persons[i], count.getOrDefault(persons[i], 0)+1);
15             if(lead == -1 || count.get(persons[i]) >= count.get(lead)){
16                 lead = persons[i];
17             }
18             
19             this.cachedMostVote.put(times[i], lead);
20         }
21     }
22     
23     public int q(int t) {
24         return cachedMostVote.get(getFloorKey(t));
25     }
26     
27     private int getFloorKey(int t){
28         int l = 0;
29         int r = times.length-1;
30         while(l <= r){
31             int mid = l + (r-l)/2;
32             if(times[mid] == t){
33                 return times[mid];
34             }else if(times[mid] < t){
35                 l = mid+1;
36             }else{
37                 r = mid-1;
38             }
39         }
40         
41         return times[r];
42     }
43 }
44 
45 /**
46  * Your TopVotedCandidate object will be instantiated and called as such:
47  * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
48  * int param_1 = obj.q(t);
49  */

 

posted @ 2019-11-24 13:30  Dylan_Java_NYC  阅读(185)  评论(0编辑  收藏  举报