-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdota2-senate.py
168 lines (155 loc) · 5.51 KB
/
dota2-senate.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 649. Dota2 Senate
# 🟠 Medium
#
# https://leetcode.com/problems/dota2-senate/
#
# Tags: String - Greedy - Queue
import timeit
from collections import Counter, deque
# Use two queues, one for each group of senators, store their index in
# the queues, for each vote, check who is the next senator that has the
# right to vote, that senator will greedily ban the first senator in the
# other group that can vote, then will move themselves to the end of its
# group's queue.
#
# Time complexity: O(n) - The maximum number of times the senators can
# vote, which is the same as the number of times the while loop will
# run, is equal to the number of senators, each round half the senators
# get to vote and remove half of the remaining ones.
# Space complexity: O(n) - The size of the queues.
#
# Runtime 72 ms Beats 45.87%
# Memory 16.8 MB Beats 5.50%
class Solution:
def predictPartyVictory(self, senate: str) -> str:
d, r = deque(), deque()
for i, member in enumerate(senate):
if member == "D":
d.append(i)
else:
r.append(i)
# Each senator will ban the first senator of the opposite party
# that can vote to prevent them from voting.
next_idx = 0
while d and r:
# Reset the index for the next round.
if next_idx > d[0] and next_idx > r[0]:
next_idx = 0
if next_idx > d[0]:
d.popleft()
r.append(r.popleft())
next_idx = r[-1] + 1
continue
if next_idx > r[0]:
r.popleft()
d.append(d.popleft())
next_idx = d[-1] + 1
continue
# Both indexes have not voted this round yet.
if d[0] < r[0]:
r.popleft()
d.append(d.popleft())
next_idx = d[-1] + 1
else:
d.popleft()
r.append(r.popleft())
next_idx = r[-1] + 1
return "Dire" if d else "Radiant"
# Use a single queue, keep track of the number of banned senators from
# each group. Pop the next senator in the queue, if it belongs to a
# group that has banned senators, remove it from the queue and continue,
# otherwise, add it to the back of the queue and increase the number of
# banned senators from the opposing group.
#
# Time complexity: O(n) - The maximum number of times the senators can
# vote, which is the same as the number of times the while loop will
# run, is equal to the number of senators, each round half the senators
# get to vote and remove half of the remaining ones.
# Space complexity: O(n) - The size of the queue.
#
# Runtime 70 ms Beats 48.62%
# Memory 16.4 MB Beats 11.62%
class Solution2:
def predictPartyVictory(self, senate: str) -> str:
cd, cr = 0, 0
for s in senate:
if s == "D":
cd += 1
else:
cr += 1
q = deque(senate)
bd, br = 0, 0
while cd and cr:
s = q.popleft()
if s == "D":
# Was this senator banned by a previous one?
if bd > 0:
bd -= 1
cd -= 1
continue
# If not banned, move it to the end and ban a Radiant.
q.append(s)
br += 1
else:
if br > 0:
br -= 1
cr -= 1
continue
q.append(s)
bd += 1
return "Dire" if cd else "Radiant"
# Use a single queue, keep track of the number of banned senators from
# each group. Pop the next senator in the queue, if it belongs to a
# group that has banned senators, remove it from the queue and continue,
# otherwise, add it to the back of the queue and increase the number of
# banned senators from the opposing group.
#
# Time complexity: O(n) - The maximum number of times the senators can
# vote, which is the same as the number of times the while loop will
# run, is equal to the number of senators, each round half the senators
# get to vote and remove half of the remaining ones.
# Space complexity: O(n) - The size of the queue.
#
# Runtime 87 ms Beats 33.64%
# Memory 16.4 MB Beats 11.62%
class Solution3:
def predictPartyVictory(self, senate: str) -> str:
c, q = Counter(senate), deque(senate)
b = {"R": 0, "D": 0}
while c["D"] and c["R"]:
s = q.popleft()
if b[s] > 0:
b[s] -= 1
c[s] -= 1
else:
q.append(s)
b["D" if s == "R" else "R"] += 1
return "Dire" if c["D"] else "Radiant"
def test():
executors = [
Solution,
Solution2,
Solution3,
]
tests = [
["DR", "Dire"],
["RDD", "Dire"],
["RD", "Radiant"],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.predictPartyVictory(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()