Grid paint
MediumOnline Assessment
Paint rectangles onto a grid and return the final picture.
Start with a grid of rows rows and cols columns where every cell is 0. Each stamp is [top, left, height, width, value] and sets every cell with row in [top, top + height) and column in [left, left + width) to value. Parts of a stamp that fall outside the grid are ignored (clipped), and top/left may be negative. A stamp with height or width of 0 changes nothing. Apply stamps in order, so later stamps overwrite earlier ones.
Input: rows and cols (positive integers) and stamps, a list of 5-integer lists.
Output: the grid as a list of rows lists, each with cols integers (row 0 is the top row).
Example 1
- Input:
- [2,3,[[0,1,2,2,5],[1,2,1,1,9]]]
- Output:
- [[0,5,5],[0,5,9]]
- Explanation
- The first stamp fills rows 0-1 and columns 1-2 with 5. The second stamp then overwrites the single cell at row 1, column 2 with 9.
Example 2
- Input:
- [2,2,[[-1,-1,2,2,7]]]
- Output:
- [[7,0],[0,0]]
- Explanation
- The stamp starts at (-1, -1) and is 2x2, so only its bottom-right cell lands on the grid, at row 0, column 0. The rest is clipped.
Example 3
- Input:
- [3,4,[[0,0,3,2,1],[1,1,1,3,2],[2,3,5,5,4]]]
- Output:
- [[1,1,0,0],[1,2,2,2],[1,1,0,4]]
- Explanation
- Stamp 1 fills columns 0-1 in every row with 1. Stamp 2 overwrites row 1, columns 1-3 with 2. Stamp 3 starts at row 2, column 3 and is clipped to that single cell, which gets 4.
Constraints
1 <= rows, cols <= 1000 <= stamps.length <= 100-100 <= top, left, value <= 100and0 <= height, width <= 100