1264. Deepest Sheet Stack
A print shop lays rectangular paper sheets flat on a very large table. Sheet i is given as [x1, y1, x2, y2] and covers the half-open region from x1 up to but not including x2, and from y1 up to but not including y2, with x1 < x2 and y1 < y2. Sheets that only touch along an edge or at a corner do not overlap.
The stack depth at a point is the number of sheets covering it. Given the array sheets, return the largest stack depth over all points of the table.
Coordinates are large but there are at most 300 sheets, so compress the coordinates. Aim for O(n^2) time and space after sorting, using a two-dimensional difference array.
Example 1
- Input:
- sheets = [[0,0,4,4],[2,2,6,6],[3,3,5,5]]
- Output:
- 3
- Explanation:
The point (3, 3) lies in all three sheets, so the deepest stack is 3.
Example 2
- Input:
- sheets = [[0,0,2,2],[2,0,4,2]]
- Output:
- 1
- Explanation:
The sheets only share the edge x = 2, which neither covers from both sides, so no point is covered twice and the answer is 1.
Example 3
- Input:
- sheets = [[1,1,5,2],[2,0,3,9],[0,0,10,10]]
- Output:
- 3
- Explanation:
Around (2, 1) the first, second and third sheets all overlap, so the deepest stack is 3.
Constraints
1 ≤ n ≤ 300
0 ≤ coordinates ≤ 109
x1 < x2 and y1 < y2 for every sheet.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n^2)
- Space
- O(n^2)