A/C Python solution, easy to understand, beat 15%


  • 0
    W
    def __init__(self):
        self.res = ()
    def findContestMatch(self, n):
        """
        :type n: int
        :rtype: str
        """
        #print "n = ", n
    
        teamArray = [i for i in range(1, n+1)]
        #print "teamArray = ", teamArray
    
    
        def generateHalf(remainingTeamArray):
            #print "remainingTeamArray = ", remainingTeamArray #[1,2,3,4,...]
            lenRemainingTeamArrray = len(remainingTeamArray)
    
            half1, half2 = [], []
            tmpArray = []
    
            #while (len(half1) !=1 ) and (len(half2) != 1):
            while (len(remainingTeamArray) != 1):
                curHalf = 0
                #print "0 curHalf = ", curHalf
                #print "0 remainingTeamArray = ", remainingTeamArray
                #print "0 tmpArray = ", tmpArray
                #print "0 half1 = ", half1
                #print "0 half2 = ", half2
    
                while (len(remainingTeamArray) > 1):
                    #print "before pop remainingTeamArray = ", remainingTeamArray
    
                    if len(remainingTeamArray) < 2:
                        #print "0 break"
                        break
                    else:
                        teamFirst = remainingTeamArray.pop(0)
                        teamLast = remainingTeamArray.pop()
                        teamMatch = (teamFirst, teamLast)
                        #print "teamFirst = ", teamFirst, " teamLast = ", teamLast
                        #print "teamMatch = ", teamMatch
                        #print "1 half1 = ", half1
                        #print "1 half2 = ", half2
                        #print "after pop, remainingTeamArray = ", remainingTeamArray
    
                        if len(half1) > len(half2):
                            #print "1 half2 append"
                            half2.append(teamMatch)
                            tmpArray.append(teamMatch)
                        elif len(half1) < len(half2):
                            #print "1 half1 append"
                            half1.append(teamMatch)
                            tmpArray.append(teamMatch)
                        elif len(half1) == len(half2):
                            if curHalf == 0:
                                #print "2 half1 append"
                                half1.append(teamMatch)
                                tmpArray.append(teamMatch)
                                curHalf = 1
                            elif curHalf == 1:
                                #print "2 half2 append"
                                half2.append(teamMatch)
                                tmpArray.append(teamMatch)
                                curHalf = 0
    
                    # print "2 half1 = ", half1
                    # print "2 half2 = ", half2
                    # print "2 tmpArray = ", tmpArray
                    # print "2 remainingTeamArray = ", remainingTeamArray
    
                remainingTeamArray = tmpArray
                # print "after add remainingTeamArray = ", remainingTeamArray
                # print "len(remainingTeamArray) = ", len(remainingTeamArray)
                half1, half2, tmpArray = [], [], []
                self.res = remainingTeamArray
                #print "res = ", self.res
    
    
        generateHalf(teamArray)
    
        # print "in the end, self.res = ", self.res
        str1 = ''.join(str(e) for e in self.res)
    
        # print "str1.replace(" ", "") = ", str1.replace(" ", "")
        return str1.replace(" ", "")

Log in to reply
 

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