MCPcopy
hub / github.com/keon/algorithms / reconstruct_queue

Function reconstruct_queue

algorithms/queue/reconstruct_queue.py:18–35  ·  view source on GitHub ↗

Reconstruct the queue from (height, k) pairs. Args: people: List of [height, k] pairs. Returns: The reconstructed queue as a list of [height, k] pairs. Examples: >>> reconstruct_queue([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]) [[5, 0], [7, 0], [5

(people: list[list[int]])

Source from the content-addressed store, hash-verified

16
17
18def reconstruct_queue(people: list[list[int]]) -> list[list[int]]:
19 """Reconstruct the queue from (height, k) pairs.
20
21 Args:
22 people: List of [height, k] pairs.
23
24 Returns:
25 The reconstructed queue as a list of [height, k] pairs.
26
27 Examples:
28 >>> reconstruct_queue([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]])
29 [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
30 """
31 queue: list[list[int]] = []
32 people.sort(key=lambda x: (-x[0], x[1]))
33 for height, count in people:
34 queue.insert(count, [height, count])
35 return queue

Callers 1

Calls 1

insertMethod · 0.45

Tested by 1