zoukankan      html  css  js  c++  java
  • 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  */
  • 相关阅读:
    java中goto语句
    随机产生一个小写字母
    java移位操作符注意的问题
    +号操作符
    类名引用static变量好处
    自己做题的简单的算法
    read()方法读取的是一个字节,为什么返回是int,而不是byte
    一道判断题
    关于继承中静态代码块,构造代码块,构造函数执行顺序
    比较好的Redux和React-Redux学习资料
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11922130.html
Copyright © 2011-2022 走看看