Python


  • 0
    B

    Its long and slow n^2 (compared to other accepted solutions) but the closest I would have come up in an interview.

    class Solution(object):
        def reconstructQueue(self, people):
            """
            :type people: List[List[int]]
            :rtype: List[List[int]]
            """
            if len(people) < 1: return []
            retVal = []
            people.sort(key=lambda x: x[1])
            for p in people:
                    if len(retVal) == 0: 
                            retVal.append(p)
                            continue
                    cnt  = left = 0
                    while(True):
                            if cnt > len(retVal) -1: 
                                    retVal.append(p)
                                    break
                            if p[1] <= left and p[0] <= retVal[cnt][0]:
                                    retVal.insert(cnt, p)
                                    break
                            if p[0] <= retVal[cnt][0]:
                                    left += 1
                            cnt += 1 
    
            return retVal
    

Log in to reply
 

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