成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 911. Online Election

SoapEye / 680人閱讀

Problem

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, 10^9].
TopVotedCandidate.q is called at most 10000 times per test case.
TopVotedCandidate.q(int t) is always called with t >= times[0].

Solution
class TopVotedCandidate {

    Map history = new HashMap<>();
    int[] times;
    public TopVotedCandidate(int[] persons, int[] times) {
        this.times = times;
        Map votes = new HashMap<>();
        int n = persons.length;
        int leader = persons[0];
        for (int i = 0; i < n; i++) {
            votes.put(persons[i], votes.getOrDefault(persons[i], 0)+1);
            if (votes.get(persons[i]) >= votes.get(leader)) leader = persons[i];
            history.put(times[i], leader);
        }
    }
    
    public int q(int t) {
        int i = Arrays.binarySearch(times, t);
        if (i < 0) return history.get(times[-i-2]);
        return history.get(times[i]);
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/72570.html

相關文章

  • leetcode7:漢明距離

    摘要:題目漢明距離是兩個字符串對應位置的不同字符的個數(shù),這里指二進制的不同位置例子我的解法先將,進行異位或運算再轉化成二進制然后把去掉算出長度其他方法先算出不同位數(shù),然后用右移運算符算出能右移幾次來獲取距離 1題目 The Hamming distance between two integers is the number of positions at which the corresp...

    xeblog 評論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • 2.leetcode唯一的摩斯密碼

    摘要:題目自己的解決方法其他解決方法 1.題目International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: a maps to .-, b maps to -..., c maps to -.-., and...

    FreeZinG 評論0 收藏0
  • 9 .leetcode Peak Index in a Mountain Array

    摘要:題目例子我的解法其他解法求最大值然后求二分法查找 1 題目 Lets call an array A a mountain if the following properties hold: A.length >= 3There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[...

    txgcwm 評論0 收藏0
  • 8.leetcode Self Dividing Numbers

    摘要:題目例子我的解法其他解法這個方法不用轉化成字符串,直接得到的數(shù)再除 1. 題目 A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 ...

    付永剛 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<