<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:
        # m = max([i[1] for i in tiles])
        # arr = [0 for i in range(m)] 
        # tiles.sort(key = lambda x : x[0] )
        # for l, r in tiles:
        #     for i in range(l, r+1): 
        #         if arr[i-1] != 1:
        #             arr[i-1] = 1
        
        # l = 0
        # curcount = 0
        # ans = 0
        # for r, v in enumerate(arr):
        #     if v == 1:
        #         curcount += 1
        #     while (r-l+1) > carpetLen:
        #         if arr[l] == 1:
        #             curcount -= 1
        #         l += 1
        #     ans = max(curcount, ans)
        # return ans
   
        tiles.sort(key = lambda x : x[0] ) 
        left = 0
        curtotal = 0 
        ans = 0
        for tl, tr in tiles:
            curtotal += tr-tl+1

            # while tiles[left][1] < tr - carpetLen + 1: #make sure ending of left tile is within range
            while carpetLen < tr - tiles[left][1] + 1: #dist between ending of curtiles and ending of l tiles, if over carpetLen, then impossible to make carpetlen -> shrink
                curtotal -= tiles[left][1] - tiles[left][0] + 1
                left += 1
            # (tr - tiles[left][0] + 1 - carpetLen) -> total_tiles - acceptable_range_tiles(# of white determined by curtotal) -> extra non-white-tiles that needs to be deleted
            # ans = max(ans, curtotal - max(tr - carpetLen + 1 - tiles[left][0], 0))

            check = tr - carpetLen + 1 - tiles[left][0] #we know ending of left tile is within acctpable range(tr - carpetLen + 1), check if beginning is, if it isnt, need to subtract beginning tiles(which are not white/within range)

            #make sure beginning of left tile is within range, if not subtract extra tiles in the beginning i.e. [8-10, 10-18], w = 10 -> acceptable start is at 9[18-10+1] but start of first range is at 8, so subtract 9-8=1 of extra tiles not in window
            if check > 0:
                ans = max(ans, curtotal - check)
            else: #if beginning of left tile is within range, dont need to do anything
                ans = max(ans, curtotal)

        return ans
827