Talentd
FleetCode

2271. Maximum White Tiles Covered by a Carpet

Medium36.9% acceptance3 companies

You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white.

You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.

Return the maximum number of white tiles that can be covered by the carpet.

Example 1:

Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
Output: 9
Explanation: Place the carpet starting on tile 10. 
It covers 9 white tiles, so we return 9.
Note that there may be other places where the carpet covers 9 white tiles.
It can be shown that the carpet cannot cover more than 9 white tiles.

Example 2:

Input: tiles = [[10,11],[1,1]], carpetLen = 2
Output: 2
Explanation: Place the carpet starting on tile 10. 
It covers 2 white tiles, so we return 2.

Constraints:

  • 1 <= tiles.length <= 5 * 104
  • tiles[i].length == 2
  • 1 <= li <= ri <= 109
  • 1 <= carpetLen <= 109
  • The tiles are non-overlapping.

Asked at 3 companies

Hints open one at a time

Hint 1
Think about the potential placements of the carpet in an optimal solution.
Hint 2
Can we use Prefix Sum and Binary Search to determine how many tiles are covered for a given placement?
18:08
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,5],[10,11],[12,18],[20,25],[30,32]]
10
Input
[[10,11],[1,1]]
2
Your code is saved on this device.Solution