Skip to the code
cf.
+25 XP

#731 Masonry for a Huge Gallery

medium

A wall display shows a gallery of up to 100,000 tiles in up to 10,000 narrow columns. Tiles are placed in order at the bottom of the currently shortest column; ties go to the smallest column index. There are no gaps.

Write placeTiles(heights, columns) that returns the column index of each tile. Scanning every column for every tile would be too slow at this size.

Example 1

Input: heights = [5,3,4,1,2], columns = 2
Output: [0,1,1,0,0]

Column heights go [5, 3], [5, 7], [6, 7], and the last tile also goes to column 0 because 6 is less than 7.

Example 2

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

With more columns than tiles, each tile takes the next empty column.

Constraints

  • 0 <= heights.length <= 10^5, 1 <= heights[i] <= 10^4
  • 1 <= columns <= 10^4
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).