-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path911.py
45 lines (39 loc) · 1.59 KB
/
911.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
__________________________________________________________________________________________________
sample 692 ms submission
from collections import defaultdict
from bisect import bisect_right as br
class TopVotedCandidate:
def __init__(self, persons: List[int], times: List[int]):
self.counter = defaultdict(int)
self.curr_winner = []
self.times = times
curr_max = 0
for p in persons:
self.counter[p] += 1
if self.counter[p] >= curr_max:
curr_max = self.counter[p]
self.curr_winner.append(p)
else:
self.curr_winner.append(self.curr_winner[-1])
def q(self, t: int) -> int:
i = br(self.times, t)
return self.curr_winner[i-1]
# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)
__________________________________________________________________________________________________
sample 700 ms submission
class TopVotedCandidate:
def __init__(self, persons, times):
votes = collections.defaultdict(int)
winner = 0
self.winners = [None] * len(times)
self.times = times
for i, person in enumerate(persons):
votes[person] += 1
if votes[person] >= votes[winner]:
winner = person
self.winners[i] = winner
def q(self, t):
return self.winners[bisect.bisect(self.times, t) - 1]
__________________________________________________________________________________________________