Assign Pins to the Shortest Column
Updated
Problem 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
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Task SchedulerAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Defer Expenses to Preserve BalanceDRW · PHONE SCREENDRW interview questions
- Minimum Bus Fleet Across StationsSnowflake · PHONE SCREENSnowflake interview questions
- Minimize Array Sum After K Halving OperationsDocuSign · PHONE SCREENDocuSign interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Furthest Building with Sandbags and RopesZip · PHONE SCREENZip interview questions
- Minimum Sum After K Ceiling HalvingsAdobe · PHONE SCREENAdobe interview questions
- Furthest Building with Sandbags and RopesBloomberg LP · PHONE SCREENBloomberg LP interview questions