Paint Stacked Boxes onto a Character Grid
Arrays & HashingAuto-graded · 21 test casesLast 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_wandbox_hare the box's width (columns) and height (rows). The box covers columnslefttoleft + box_w - 1and rowstoptotop + 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:
- Boxes are painted in list order. When a cell is covered by several boxes, it keeps the character from the one painted last.
- A box may extend partly or entirely beyond the grid, and
leftandtopmay 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. - A box with
box_w == 1orbox_h == 1consists only of edge cells. - 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 <= 1000grid_w * grid_h <= 10^61 <= box_w, box_h <= 1000leftandtopare integers and may be negative.edge_charandinner_charare 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Keep the grid as a mutable list of lists of characters and paint the boxes one by one in the given order. Convert each row to a string at the end.
For each box:
- Clip the visiting range. The box covers columns
[left, left + box_w)and rows[top, top + box_h). Intersect these with[0, grid_w)and[0, grid_h). If either intersection is empty, the box is invisible and is skipped. - Classify each visible cell using the full box. For a cell
(r, c), computedr = r - topanddc = c - left. It is an edge cell exactly whendris0orbox_h - 1, ordcis0orbox_w - 1. Otherwise it is an inner cell. - Assign. Writing to the cell replaces whatever was there, which gives the "later box wins" rule automatically.
Why it is correct
- Every cell that is both inside the box and inside the grid lies in the clipped ranges, so it gets painted. No other cell is touched.
- Both loop ranges are subsets of the valid grid indices, so indexing never fails. Empty intersections are skipped, which covers boxes completely off-grid.
- Edge status uses offsets from the true corner. A box that continues past the grid boundary therefore shows no edge there. For example, with
left = -2, grid column 0 hasdc = 2, so it is an inner cell unless the box is narrower. - When
box_w == 1,dc == 0always holds, and whenbox_h == 1,dr == 0always holds. Thin boxes are therefore all edge, with no special case. - Since boxes are applied sequentially, each cell finally holds the value from the last box covering it.
- Rows always have exactly
grid_wcharacters because they are built from a list of that length.grid_h == 0gives an empty list andgrid_w == 0gives empty strings.
Complexity
Let W = grid_w, H = grid_h, and let A_i be the visible (clipped) area of box i.
- Time:
O(W * H + sum of A_i). This is at mostO(W * H * B)forBboxes, but a box that is mostly off-grid costs only its visible part. - Space:
O(W * H)for the grid and the output.
Reference solution
from typing import List
def paint_boxes(grid_w: int, grid_h: int, boxes: List[List]) -> List[str]:
grid = [[' '] * grid_w for _ in range(grid_h)]
for left, top, box_w, box_h, edge_char, inner_char in boxes:
col_start = max(0, left)
col_end = min(grid_w, left + box_w)
row_start = max(0, top)
row_end = min(grid_h, top + box_h)
if col_start >= col_end or row_start >= row_end:
continue
for r in range(row_start, row_end):
dr = r - top
on_row_edge = dr == 0 or dr == box_h - 1
row = grid[r]
for c in range(col_start, col_end):
dc = c - left
if on_row_edge or dc == 0 or dc == box_w - 1:
row[c] = edge_char
else:
row[c] = inner_char
return [''.join(row) for row in grid]