Python Recursive Code


  • 0
    H

    Given that the sum of two matching team numbers in the first round is always equal to pow(2,depth)+1, the idea is to keep replacing team numbers until the depth reaches the total number of rounds.

    class Solution(object):
        def helper(self, num, depth, k):
            if depth == k:
                return "(" + str(num) + "," + str(2**depth+1-num) + ")"
            return "(" + self.helper(num, depth+1, k) + "," + self.helper(2**depth+1-num, depth+1, k) + ")"
                
        def findContestMatch(self, n):
            """
            :type n: int
            :rtype: str
            """
            # calculate the total number of rounds needed
            k = 0 
            while n > 1:
                n >>= 1
                k += 1
            return self.helper(1, 1, k)
    

Log in to reply
 

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