Rectangle Point Coverage Counts
Problem statement
You are given a grid of cells that is width cells wide and height cells tall. Cell (x, y) is in column x and row y, where 0 <= x < width and 0 <= y < height. At the start, the grid holds no rectangles.
Process the strings in operations in order. Each one is one of these:
"ADD id x1 y1 x2 y2": add a rectangle namedid. It covers every cell(x, y)withx1 <= x <= x2andy1 <= y <= y2, so its corner and edge cells are covered too."REMOVE id": remove the rectangle namedid. It no longer covers any cell."COUNT_AT x y": count how many rectangles currently in the grid cover cell(x, y).
Rectangles may overlap, and two rectangles may cover exactly the same cells. Each one counts on its own. IDs are case-sensitive, so a and A are different IDs. Once a rectangle is removed, its id is free again, and a later ADD may use it for a new rectangle.
Examples
Example 1
width = 5height = 4operations = ["ADD a 0 0 2 1","ADD b 1 1 4 3","COUNT_AT 1 1","COUNT_AT 2 0","COUNT_AT 4 3","COUNT_AT 0 3","REMOVE a","COUNT_AT 1 1"]return = [2,1,1,0,1]Rectangle a covers columns 0 to 2 of rows 0 and 1. Rectangle b covers columns 1 to 4 of rows 1 to 3.
- Cell
(1, 1)is inside both rectangles, so the answer is2. - Cell
(2, 0)is insideaonly, becausebstarts at row1. The answer is1. - Cell
(4, 3)is the far corner ofb. Corner cells are covered, so the answer is1. - Cell
(0, 3)is in neither rectangle, so the answer is0. - After
REMOVE a, onlybcovers cell(1, 1), so the answer is1.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.