Simple Python


  • 0

    The idea is simple, catch the ball before the bound. Whenever a team ban another team, or I'd rather use kill here, we accumulate the total killing number. As long as there is available kill, we kill the current alive player and move to the next alive player.

    class Solution(object):
        def predictPartyVictory(self, senate):
            """
            :type senate: str
            :rtype: str
            """
            n = len(senate)
            r, d = senate.count('R'), senate.count('D')
            alive = [1] * n
            i = kill_d = kill_r = 0
            while r and d:
                if alive[i]:
                    if senate[i] == "R":
                        if kill_r:
                            r -= 1
                            kill_r -= 1
                            alive[i] = 0
                        else:
                            kill_d += 1
                    else:
                        if kill_d:
                            d -= 1
                            kill_d -= 1
                            alive[i] = 0
                        else:
                            kill_r += 1
                i = (i+1) % n
            return ("Radiant", "Dire")[not r]
    

Log in to reply
 

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