Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Given a set of pins with attributes (pin id, height), write a function that takes the argument 'k' (determining the number of columns)
- and inserts the pins such that every pin goes into a column with least consumed height. If there is a tie then insert into the left most column.
- """
- import heapq
- class PriorityQueue:
- def __init__(self):
- self._queue = []
- def pop(self):
- return heapq.heappop(self._queue)
- def push(self, item, lst, priority):
- heapq.heappush(self._queue, (priority, lst, item))
- def arrange_pins(pins, n_col):
- position = PriorityQueue()
- for i in range(n_col):
- position.push(f'Col_{i}', [], 0)
- for pin, name in pins:
- height, lst, name_col = position.pop()
- position.push(name_col, lst + [name], height + pin)
- return position._queue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement