<p>You are given a 2D integer array <code>tiles</code> where <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents that every tile <code>j</code> in the range <code>l<sub>i</sub> &lt;= j &lt;= r<sub>i</sub></code> is colored white.</p>

<p>You are also given an integer <code>carpetLen</code>, the length of a single carpet that can be placed <strong>anywhere</strong>.</p>

<p>Return <em>the <strong>maximum</strong> number of white tiles that can be covered by the carpet</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png" style="width: 644px; height: 158px;" />
<pre>
<strong>Input:</strong> tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
<strong>Output:</strong> 9
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png" style="width: 231px; height: 168px;" />
<pre>
<strong>Input:</strong> tiles = [[10,11],[1,1]], carpetLen = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> Place the carpet starting on tile 10. 
It covers 2 white tiles, so we return 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= tiles.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>tiles[i].length == 2</code></li>
	<li><code>1 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= carpetLen &lt;= 10<sup>9</sup></code></li>
	<li>The <code>tiles</code> are <strong>non-overlapping</strong>.</li>
</ul>

class Solution:
    def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
        tiles.sort()
        result = 0
        carpet_left, carpet_right = tiles[0][0], tiles[0][0]
        curr_cover = 0
        tile_left, tile_right = 0, 0
        while carpet_right <= tiles[-1][1]:
            if tile_right >= len(tiles):
                break
            if carpet_right >= tiles[tile_right][1]:
                # print(f'before {curr_cover} cl {carpet_left} cr {carpet_right} tl {tiles[tile_left]} tr {tiles[tile_right]}')
                curr_cover += tiles[tile_right][1]-tiles[tile_right][0]+1
                # print(f'after {curr_cover} cl {carpet_left} cr {carpet_right} tl {tiles[tile_left]} tr {tiles[tile_right]}')  # after 9 cl 9 cr 18 tl [10, 11] tr [12, 18]
                result = max(result, curr_cover)
                tile_right += 1
            else:
                carpet_right = tiles[tile_right][1]
                if carpet_right - carpet_left + 1 <= carpetLen:
                    continue
                target_left = carpet_right-carpetLen+1
                if target_left <= tiles[tile_left][1]:
                    curr_cover -= (target_left-max(tiles[tile_left][0], carpet_left))
                    carpet_left = target_left
                    continue
                while tiles[tile_left][1] < target_left:
                    curr_cover -= (tiles[tile_left][1]-max(tiles[tile_left][0], carpet_left)+1)
                    tile_left += 1
                    if tile_left >= len(tiles):
                        break
                    carpet_left = max(target_left, tiles[tile_left][0])
                    curr_cover -= (carpet_left-tiles[tile_left][0])
                # if tile_left < len(tiles) and carpet_left >= tiles[tile_left][0]:
                    # curr_cover -= (carpet_left-tiles[tile_left][0])

            # if tiles[tile_right][0] <= carpet_right <= tiles[tile_right][1]:
            #     curr_cover += 1
            # result = max(result, curr_cover)
            # carpet_right += 1
            # if tile_right < len(tiles)-1 and carpet_right >= tiles[tile_right+1][0]:
            #     tile_right += 1
            # if carpet_right - carpet_left + 1 > carpetLen:
            #     if tiles[tile_left][0] <= carpet_left <= tiles[tile_left][1]:
            #         curr_cover -= 1
            #     carpet_left += 1
            # if carpet_left > tiles[tile_left][1]:
            #     tile_left += 1
        return result

887