<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, T: List[List[int]], clen: int) -> int:
        T.sort()
        #get prefix length of tile groups
        pref = [0] + list(itertools.accumulate(r - l + 1 for l, r in T))
        print(pref)
        ends = [r for _, r in T]
        print(ends)
        
        n = len(ends)
        j, ans = 0, 0
        
        #EVERY SINGLE POSSIBLE BEST SOLUTION HAS TO START AT THE BEGINNING OF SOME TILE GROUP
        #because shifting from that start of tile group only either adds a gap or tile, at the cost of a tile, so no increase in tiles
        #if there is some larger tile group later then you just start from the start of that tile group
        #https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/solutions/2038674/python-explanation-with-pictures-sliding-window/?envType=problem-list-v2&envId=a99jbdue&difficulty=MEDIUM
        for i in range(n):
            l, _ = T[i]                        # Carpet starts from the begining of each range
            r = min(ends[-1], l + clen - 1)    # The rightmost index having tile is ends[-1]

            while j < n and ends[j] < r:       # While the WHOLE current range is covered by carpet
                j += 1
            
            # Two cases: 
            if T[j][0] > r:    # If the right end of the carpet doesn't reach the j-th range.
                ans = max(ans, pref[j] - pref[i])
            else:              # If the right end of the carpet covers part of the j-th range.
                ans = max(ans, pref[j + 1] - pref[i] - ends[j] + r)
            
        return ans
895