Tarp pile tiles
MediumOnline Assessment
Count the courtyard tiles that are covered by at least k tarps.
A courtyard is an enormous grid of unit tiles. Tile (x, y) is the square whose corners run from (x, y) to (x + 1, y + 1). After a storm warning, crews throw tarps over it. Each tarp is given as [x1, y1, x2, y2] and covers exactly the tiles (x, y) with x1 <= x < x2 and y1 <= y < y2. The far edges are not included, so two tarps that merely share a border cover no tile in common, and every tarp covers at least one tile.
Tarps are simply laid on top of one another; a tile can be under many tarps at once. A tile counts as well covered when it lies under at least k tarps.
Input:
tarps: a list of[x1, y1, x2, y2]tarps (possibly empty).k: a positive integer.
Output: an integer, the number of well-covered tiles. If no tile qualifies (including when there are no tarps), return 0.
The courtyard is far too big to visit tile by tile, so you will need to work with the tarp edges instead.
Example 1
- Input:
- [[[0,0,4,4],[2,2,6,6]],2]
- Output:
- 4
- Explanation
- The tarps cover
xin 0..3,yin 0..3 andxin 2..5,yin 2..5. Withk = 2, only the shared 2 by 2 patch (xandyboth in 2..3) is under both tarps, so the answer is 4.
Example 2
- Input:
- [[[0,0,3,3],[1,1,4,4],[2,0,5,3]],2]
- Output:
- 7
- Explanation
- Tiles under at least two of the three tarps are (1,1), (2,1), (3,1), (1,2), (2,2), (3,2) and (2,0). Tiles under only one tarp, such as (0,0), do not count. The answer is 7.
Example 3
- Input:
- [[[0,0,2,2],[2,0,4,2]],1]
- Output:
- 8
- Explanation
- The tarps touch along the line
x = 2but, because the far edge is excluded, no tile is under both. Withk = 1every covered tile counts: 4 + 4 = 8.
Constraints
0 <= tarps.length <= 4000 <= x1 < x2 <= 10,000and0 <= y1 < y2 <= 10,000for every tarp1 <= k <= 400