<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>

<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>

<ul>
	<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 &lt;= prizePositions[i] &lt;= 3</code> or <code>2 &lt;= prizePositions[i] &lt;= 4</code>.</li>
</ul>

<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
<strong>Output:</strong> 2
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes. 
</pre>

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

<ul>
	<li><code>1 &lt;= prizePositions.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= prizePositions[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>9</sup> </code></li>
	<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
</ul>

<p>&nbsp;</p>
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; 
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
</style>

class Solution:
    def maximizeWin(self, prizePositions: List[int], k: int) -> int:
        # count of prizes for all intervals
        # (count, end position of interval)        
        intervals = []
        # max stack of intervals
        # (count, end position of interval),
        # There can't be interval with bigger count before the interval
        max_beffore = []
        start_inx = 0 # start index of the current interval
        count = 0 # count of the current interval
        for inx, pos in enumerate(prizePositions):
            count += 1
            # subtract prizes from the current interval if they are not covered by the interval
            while pos-k > prizePositions[start_inx]:
                count -= 1
                start_inx += 1
            intervals.append((count, pos))
            if not max_beffore or max_beffore[-1][0] < count:
                max_beffore.append((count, pos))

        max_solution = 0
        while intervals:
            # the last interval for the current solution
            count, pos = intervals.pop()
            # max_beffore stores only intervals before the last interval,
            # max_beffore is a max stack,
            # so the top of the max possible has the max count among all values in the max_beffore
            while max_beffore and max_beffore[-1][1] >= pos-k:
                max_beffore.pop()
            # The soluthon if the current last interval is the last
            candidate = count+(0 if not max_beffore else max_beffore[-1][0])
            # we need to find maximum among all candidates
            max_solution = max(candidate, max_solution)
        return max_solution
            
40000