Assign Pins to the Shortest Column
Learn this problemProblem statement
You are given an integer array pins, where pins[i] is the height of the ith pin, and an integer k representing the number of columns. Every column starts with total height 0.
Process the pins in their original order. Append each pin to the column with the smallest current total height. If several columns have the same smallest height, choose the one with the smallest index. After a pin is appended, add its height to that column's total height.
Return a two-row long[][] matrix:
- The first row contains the assigned column index for every pin.
- The second row contains the final total height of every column.
Function
assignPinsToColumns(pins: int[], k: int) → long[][]Examples
Example 1
pins = [1,2,3,4,5]k = 2return = [[0,1,0,1,0],[9,6]]Both columns begin at height 0. The pins go to columns 0, 1, 0, 1, 0 in order. Their final heights are 1 + 3 + 5 = 9 and 2 + 4 = 6.
Example 2
pins = [10,20,30]k = 3return = [[0,1,2],[10,20,30]]All columns start tied, so the smallest-index rule assigns the three pins to columns 0, 1, and 2. The final heights are 10, 20, and 30.
Example 3
pins = [5,5,5,5,5,5]k = 2return = [[0,1,0,1,0,1],[15,15]]The columns repeatedly tie after every pair of pins. The smallest-index rule therefore gives each new pair to columns 0 and 1 in that order. Both columns finish at height 15.
Constraints
1 <= k <= pins.length1 <= pins.length <= 2 * 10^51 <= pins[i] <= 10^9