Greedy Tile Placement Across Parallel Lanes
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Simulate placing items one by one into the currently lowest lane, breaking ties by lowest index, and report each lane's final total using a min-heap.
Greedy Tile Placement Across Parallel Lanes
A gallery page displays tiles in several vertical lanes. You receive the tiles in the order they arrive, and each tile has a positive or zero height. The number of lanes is fixed and every lane starts empty with a total height of 0.
Place the tiles one at a time, in the given order, using this rule:
- Put the tile into the lane whose current total height is the smallest.
- If several lanes share that smallest total, choose the one with the smallest lane index (lanes are numbered from
0). - Adding a tile increases that lane's total height by the tile's height.
Once every tile has been placed, report the total height of each lane.
Task
Implement a function that takes the list of tile heights (in arrival order) and the number of lanes, and returns a list of length num_lanes where the entry at position i is the final total height of lane i.
Notes
- If there are no tiles, every lane has total
0. - If there are more lanes than tiles, lanes that never receive a tile stay at
0. - A tile of height
0leaves its lane's total unchanged, but it is still assigned according to the rules above. - The result must be ordered by lane index, not in any other order.
Constraints
0 <= len(tile_heights) <= 10^51 <= num_lanes <= 10^50 <= tile_heights[i] <= 10^9- Aim for better than scanning every lane for every tile.
Examples
Example 1
Input: lane_totals_after_placement([5, 2, 4, 7, 1], 3)
Output: [5, 9, 5]
Hints
Hint 1
Placing a tile means repeatedly asking for the lane with the minimum total, updating it, and putting it back. Which data structure supports this efficiently?
Hint 2
Store each lane as a pair (total_height, lane_index) in a min-heap. Pair comparison already handles the tie-break by smaller index.
Hint 3
A heap's internal array is not in lane order. After processing all tiles, write each lane's total into the result at its own lane index.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The placement rule is fully determined, so the task is a simulation. The only question is how quickly we can find the shortest lane (smallest index on ties) for each tile.
Scanning all k lanes for each of n tiles costs O(n * k). Instead, keep the lanes in a min-heap of (total_height, lane_index) pairs:
- Build the heap with
(0, i)for each lanei. - For each tile, pop the smallest pair. That lane receives the tile. Push the pair back with the total increased by the tile's height.
- After all tiles, the heap holds exactly one pair per lane. Write each total into
result[lane_index].
Why it is correct
- Invariant: before each tile, the heap contains exactly one pair per lane with that lane's current total. It holds at the start, and each step removes one pair and reinserts it updated.
- Right choice: Python compares tuples lexicographically, so the popped pair has the minimum total, and among equal totals the minimum lane index. That is exactly the required rule.
- Output order: the heap's internal order is arbitrary, so totals are placed into the result by lane index rather than read off in heap order.
- Edge cases follow naturally: no tiles gives all zeros, extra lanes remain
0, and zero-height tiles leave totals unchanged.
Complexity
- Time:
O(k + n log k)for heapify,npop/push pairs, and building the result. - Space:
O(k)for the heap and the result.
Reference solution
import heapq
from typing import List
def lane_totals_after_placement(tile_heights: List[int], num_lanes: int) -> List[int]:
lanes = [(0, i) for i in range(num_lanes)]
heapq.heapify(lanes)
for h in tile_heights:
total, idx = heapq.heappop(lanes)
heapq.heappush(lanes, (total + h, idx))
result = [0] * num_lanes
for total, idx in lanes:
result[idx] = total
return result