Draw Overlapping ASCII Rectangles
Learn this problemProblem statement
Draw a sequence of ASCII rectangles on a finite canvas. The canvas has canvasWidth columns and canvasHeight rows and is initially filled with the one-character string background. Coordinates use a top-left origin: x increases to the right and y increases downward.
Each rectangles[i] is [x, y, width, height]. Its boundary uses borderChars[i], while cells strictly inside the boundary use fillChars[i]. A rectangle whose width or height is 1 consists entirely of boundary cells.
Draw rectangles in input order. A later rectangle overwrites every earlier character at each canvas cell it covers. Clip drawing to the canvas: cells of a rectangle outside the canvas are ignored, but whether a visible cell is boundary or interior is determined from the rectangle's original, unclipped dimensions.
Return the final canvas as canvasHeight strings ordered from top row to bottom row, each containing exactly canvasWidth characters.
Function
drawRectangles(canvasWidth: int, canvasHeight: int, background: String, rectangles: int[][], borderChars: String[], fillChars: String[]) → String[]Examples
Example 1
canvasWidth = 6canvasHeight = 5background = "."rectangles = [[1,1,4,3]]borderChars = ["#"]fillChars = ["+"]return = ["......",".####.",".#++#.",".####.","......"]The rectangle occupies columns 1 through 4 and rows 1 through 3. Its outer cells use #, and its two interior cells use +.
Example 2
canvasWidth = 7canvasHeight = 5background = "."rectangles = [[1,1,5,3],[4,0,4,4]]borderChars = ["#","@"]fillChars = ["+","o"]return = ["....@@@",".###@oo",".#++@oo",".###@@@","......."]The second rectangle is drawn last, so it overwrites the first rectangle in their overlap. Its rightmost column lies outside the canvas and is clipped; the remaining visible cells keep their boundary or interior role from the original rectangle.
Example 3
canvasWidth = 4canvasHeight = 3background = "_"rectangles = [[-2,-1,3,3],[2,1,1,2]]borderChars = ["X","|"]fillChars = ["o","!"]return = ["X___","X_|_","__|_"]Only the right boundary of the first rectangle reaches the canvas. The second rectangle has width 1, so both of its visible cells use its boundary character.
Constraints
1 <= canvasWidth, canvasHeightandcanvasWidth * canvasHeight <= 2 * 10^5.0 <= rectangles.length <= 10^4.- Each
rectangles[i]is[x, y, width, height], where-10^9 <= x, y <= 10^9and1 <= width, height <= 10^9. borderChars.length == fillChars.length == rectangles.length.background, everyborderChars[i], and everyfillChars[i]is a one-character printable ASCII string other than a line break.- For every rectangle,
borderChars[i] != fillChars[i]. - Rectangles are drawn in input order and clipped to the canvas.