#2271

Maximum White Tiles Covered by a Carpet

expert · 1060 · lc medium +32 · verified · 35.7% accepted · 839 likes · top 13%

Description

You are given a 2D integer array tiles where tiles[i] = [li, ri] means that every integer position j in the closed interval [li, ri] is a white tile.

You also have a carpet of length carpetLen that can be placed at any position on the number line.

Return the maximum number of white tiles the carpet can cover.

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.

Code

1
2
3