KatInterview

Paint Stacked Boxes onto a Character Grid

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingsimulation2d-gridclipping

Last updated

Simulate painting bordered, filled boxes onto a fixed-size text grid, clipping to the grid while keeping border and interior status based on the full box.

Paint Stacked Boxes onto a Character Grid

You are given a text grid with grid_w columns and grid_h rows. Every cell starts as a single space character ' '. You are also given a list of boxes to paint on it, one after another.

Each box is a list [left, top, box_w, box_h, edge_char, inner_char]:

  • (left, top) is the position of the box's upper-left cell. Columns increase to the right and rows increase downward. Both are zero-based, and the grid's upper-left cell is (0, 0).
  • box_w and box_h are the box's width (columns) and height (rows). The box covers columns left to left + box_w - 1 and rows top to top + box_h - 1.
  • A cell of the box is an edge cell if it lies in the box's first or last column, or in its first or last row. Paint edge cells with edge_char.
  • All other cells of the box are inner cells. Paint them with inner_char.

Rules:

  1. Boxes are painted in list order. When a cell is covered by several boxes, it keeps the character from the one painted last.
  2. A box may extend partly or entirely beyond the grid, and left and top may be negative. Only cells inside the grid are painted. Whether a visible cell is an edge or inner cell is decided by its position within the entire, unclipped box. The grid boundary never creates an edge.
  3. A box with box_w == 1 or box_h == 1 consists only of edge cells.
  4. A box that does not overlap the grid at all has no effect.

Return the final grid as a list of grid_h strings, each of exactly grid_w characters. Keep trailing spaces and never strip them. If grid_h is 0, return an empty list. If grid_w is 0 and grid_h > 0, return grid_h empty strings.

Constraints

  • 0 <= grid_w, grid_h <= 1000
  • grid_w * grid_h <= 10^6
  • 1 <= box_w, box_h <= 1000
  • left and top are integers and may be negative.
  • edge_char and inner_char are each a single printable character. They may be equal.
  • The list of boxes may be empty.

Examples

Example 1

Input: paint_boxes(5, 4, [[1, 1, 3, 2, '#', '.']])

Output: [' ', ' ### ', ' ### ', ' ']

Example 2

Input: paint_boxes(7, 5, [[1, 1, 5, 3, '#', '.'], [3, 0, 3, 4, '@', '+']])

Output: [' @@@ ', ' ##@+@ ', ' #.@+@ ', ' ##@@@ ', ' ']

Example 3

Input: paint_boxes(6, 4, [[-2, -1, 5, 4, 'X', 'o'], [1, 1, 4, 2, 'A', 'b']])

Output: ['ooX ', 'oAAAA ', 'XAAAA ', ' ']

Hints

Hint 1

Process the boxes strictly in order and keep the grid as a list of lists of characters, joining each row into a string only at the end.

Hint 2

For each box, intersect its column range [left, left + box_w) and row range [top, top + box_h) with the grid ranges, and only visit cells in that intersection. If either intersection is empty, skip the box.

Hint 3

Decide edge versus inner using offsets from the box's real upper-left corner (col - left, row - top), compared with 0 and box_w - 1 or box_h - 1, not using the clipped boundaries.