Skip to the code
cf.
+10 XP

#729 Pin Board Columns

easy

A pin board places cards of different heights into columns columns. Cards are placed in order, each at the bottom of the column that is currently shortest. If several columns are equally short, the leftmost one (smallest index) is used.

Write pinColumns(heights, columns) that returns, for each card, the index of the column it goes into. There is no gap between cards.

Example 1

Input: heights = [200,120,160,80,140], columns = 3
Output: [0,1,2,1,2]

The first three cards fill the empty columns. Then column 1 (120) is shortest, then column 2 (160 vs 200 and 200).

Example 2

Input: heights = [10,10,10,10], columns = 2
Output: [0,1,0,1]

Ties always go to the leftmost column.

Constraints

  • 0 <= heights.length <= 10^4, 1 <= heights[i] <= 10^4
  • 1 <= columns <= 20
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).