Queue Reconstruction by Height Coding Challenge June Day 6


Algorithm:

1.Sort According to their Heights in descending order.
2.if heights are same then sort them in ascending order
based on their K value.
3.insert every element in another list with k as their index.
input:l = [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
After Step 1 & 2:[[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]
After Step 2:output:[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
Python Code:

def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people = sorted(people,key = lambda a:(-a[0],a[1]))
        newlst = []
        for i in people:
             newlst.insert(i[1],i)
        return newlst



Post a Comment

0 Comments