FastPrepFastPrep
Problem Brief

Assign Pins to the Shortest Column

FULLTIMEPHONE SCREEN

You are given an array pins, where pins[i] is the height of the ith pin. There are k columns, and each column starts with total height 0.

Process the pins in their original order. For each pin, append it to the column with the smallest current total height. If multiple columns are tied for the smallest height, choose the column with the smallest index. After appending a pin, that column's total height increases by the pin's height.

Return a two-row matrix:

  • The first row contains the assigned column index for each pin.
  • The second row contains the final total height of each column.

1Example 1

Input
pins = [1,2,3,4,5], k = 2
Output
[[0,1,0,1,0],[9,6]]
Explanation

The assigned columns are [0,1,0,1,0]. Final heights are 9 and 6.

2Example 2

Input
pins = [10,20,30], k = 3
Output
[[0,1,2],[10,20,30]]

3Example 3

Input
pins = [5,5,5,5,5,5], k = 2
Output
[[0,1,0,1,0,1],[15,15]]

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= k <= pins.length
  • 1 <= pins.length <= 2 * 10^5
  • 1 <= pins[i] <= 10^9
  • Use a min-heap or equivalent data structure for efficient assignment.
public int[][] assignPinsToColumns(int[] pins, int k) {
    // write your code here
}
Input

pins

[1,2,3,4,5]

k

2

Output

[[0,1,0,1,0],[9,6]]

Sign in to submit your solution.