国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 911. Online Election

SoapEye / 670人閱讀

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]);
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72570.html

相關(guān)文章

  • leetcode7:漢明距離

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

    xeblog 評(píng)論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 評(píng)論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 評(píng)論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 評(píng)論0 收藏0
  • 8.leetcode Self Dividing Numbers

    摘要:題目例子我的解法其他解法這個(gè)方法不用轉(zhuǎn)化成字符串,直接得到的數(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 ...

    付永剛 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<