KatInterview

Greedy First-Fit Placement of Shapes on a Grid

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingsimulationmatrixgrid

Last updated

Simulate placing fixed binary shapes onto a grid at the first row-major position where their filled cells hit only empty cells, labelling each by its index.

You have an empty board with rows rows and cols columns, and an ordered list of shapes. Each shape is a rectangular binary matrix: 1 marks a cell the shape occupies and 0 marks a gap that the shape does not occupy. Shapes are placed exactly as given, with no rotation and no reflection.

Process the shapes in order, starting with shapes[0]. For shape number i (0-indexed):

  1. Consider every possible top-left corner of the shape's rectangle on the board. The whole rectangle (including its 0 cells) must lie inside the board.
  2. Try the corners in row-major order: smaller row first, and for equal rows, smaller column first.
  3. A corner is valid if every 1 cell of the shape lands on a board cell that is currently empty. A 0 cell of the shape imposes no condition, so it may sit on an already filled cell.
  4. Take the first valid corner. Write the text str(i + 1) into every board cell covered by a 1 of the shape. Each shape is placed at most once.
  5. If no corner is valid, skip this shape and leave the board unchanged. Then continue with the next shape.

Later shapes see the effects of earlier placements. After all shapes are processed, return the board as a list of rows lists, each with cols strings. Empty cells are the string ".", and filled cells hold the label of the shape that covers them (labels can have several digits).

Implement the function that returns this final board.

Constraints

  • 0 <= rows, cols, and the board is small enough for direct simulation.
  • Each shape is a non-ragged 2D list containing only 0 and 1.
  • A shape whose height exceeds rows or whose width exceeds cols can never be placed and is skipped.
  • A shape with no 1 cells writes nothing, so it has no visible effect.
  • Labels are 1-based positions in shapes, written as strings.

Examples

Example 1

Input: place_shapes(4, 5, [[[1, 1], [1, 0]], [[1, 1]], [[1], [1], [1]]])

Output: [['1', '1', '2', '2', '3'], ['1', '.', '.', '.', '3'], ['.', '.', '.', '.', '3'], ['.', '.', '.', '.', '.']]

Example 2

Input: place_shapes(2, 3, [[[1, 0, 1], [1, 1, 1]], [[1, 1]]])

Output: [['1', '.', '1'], ['1', '1', '1']]

Hints

Hint 1

Treat each shape independently: a nested loop over every top-left corner in row-major order, and stop at the first corner that works.

Hint 2

Only the 1-cells of the shape need to be checked against the board. 0-cells can overlap occupied cells without any problem, and they must never be written.

Hint 3

Limit the corners to rows 0..rows-height and columns 0..cols-width; a negative range means the shape is skipped automatically. Check all cells before writing any, so a failed attempt never leaves a partial placement.