Inclusive Rectangle Coverage Counts
Problem statement
You are given an integer n and an array rectangles. Start with an n x n matrix of zeros.
Each rectangle is represented as [top, left, bottom, right] using zero-based row and column indices. Its boundaries are inclusive. For every rectangle, add 1 to every matrix cell whose row is between top and bottom and whose column is between left and right.
Examples
Example 1
n = 3rectangles = [[0,0,1,1],[1,1,2,2]]return = [[1,1,0],[1,2,1],[0,1,1]]The two rectangles overlap only at (1,1), so that cell has count 2. Cells covered by one rectangle have count 1.
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
Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.