Rectangle Board
OpenAIPhone screenMedium
A board is an unbounded grid of integer cells, and RectangleBoard keeps the rectangles placed on it. Each rectangle has a string id, and its coordinates are inclusive: it covers every cell with x1 <= x <= x2 and y1 <= y <= y2.
Part 1: Add rectangles and count a cell
Implement:
add(rect_id, x1, y1, x2, y2)places a rectangle on the board. Returns nothing.count_at(x, y)returns how many rectangles cover the cell(x, y).
A rectangle covers every cell with x1 <= x <= x2 and y1 <= y <= y2, both ends included. Rectangles may overlap. count_at returns 0 for a cell no rectangle covers.
- Example 1
- init
[]- operations
[["add",["a",0,0,2,2]],["add",["b",1,1,3,3]],["add",["c",-2,-2,-2,-2]],["count_at",[1,1]],["count_at",[0,0]],["count_at",[3,3]],["count_at",[3,0]],["count_at",[-2,-2]],["count_at",[-2,-1]],["count_at",[2,2]]]- Output
[null,null,null,2,1,1,0,1,0,2]
Why: The rectangles a (0,0 to 2,2) and b (1,1 to 3,3) overlap on the cells 1 to 2, so (1,1) and (2,2) are covered twice. (0,0) belongs to a only and (3,3) to b only; the corner cells count because both ends are included. (3,0) is outside both. The single-cell rectangle c covers only (-2,-2).
Constraints
1 <= number of calls <= 2 * 10^5-100 <= x1 <= x2 <= 100and-100 <= y1 <= y2 <= 100- each rectangle covers at most
400cells rect_idis a string, and eachrect_idis added at most oncecount_atis asked for coordinates in-100 <= x, y <= 100
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.