649. Dota2 参议院
思路:循环队列模拟,当某一阵营人数为0截止
class Solution:
def predictPartyVictory(self, senate: str) -> str:
counter = collections.Counter(senate)
r,d = counter["R"],counter["D"]
queue = collections.deque(senate)
while(r^0 and d^0):
f = queue.popleft()
f_count = 1
while(f_count and r^0 and d^0):
s = queue.popleft()
if s==f:
queue.append(s)
f_count += 1
else:
f_count -= 1
if s=="R":
r -= 1
else:
d -= 1
queue.append(f)
return "Dire" if d^0 else "Radiant"
时间复杂度O(n),空间复杂度O(n)