Straightforward and Simple Python


  • 1
    M

    Record the position of two parties, each party will ban its upcoming opponent.

    from collections import deque
    class Solution(object):
        def predictPartyVictory(self, senate):
            """
            :type senate: str
            :rtype: str
            """
            n = len(senate)
            pos = collections.defaultdict(deque)
            for i in range(n):
            	pos[senate[i]].append(i)
    
            while pos['R'] and pos['D']:
            	if pos['R'][0] < pos['D'][0]:
            		pos['R'].append(pos['R'].popleft() + n)
            		pos['D'].popleft()
            	else:
            		pos['D'].append(pos['D'].popleft() + n)
            		pos['R'].popleft()
    
            return 'Radiant' if pos['R'] else 'Dire'
    

  • 0
    C

    I love this solution!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.