Leetcode 947 Solution

This article provides solution to leetcode question 947 (online-election)

https://leetcode.com/problems/online-election

Solution

class TopVotedCandidate(object):

    def __init__(self, persons, times):
        """
        :type persons: List[int]
        :type times: List[int]
        """
        count = collections.defaultdict(int)
        leader = None
        m = 0
        self.a = []

        for p, t in itertools.izip(persons, times):
            count[p] += 1
            c = count[p]

            if c >= m:
                if p != leader:
                    leader = p
                    self.a.append((t, p))
                m = c

    def q(self, t):
        """
        :type t: int
        :rtype: int
        """
        l = 0
        r = len(self.a)
        while l < r:
            m = (l + r) / 2
            if self.a[m][0] > t:
                r = m
            else:
                l = m + 1
        return self.a[l-1][1]


# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)