<p>There is a one-dimensional garden on the x-axis. The garden starts at the point <code>0</code> and ends at the point <code>n</code>. (i.e., the&nbsp;length of the garden is <code>n</code>).</p>

<p>There are <code>n + 1</code> taps located at points <code>[0, 1, ..., n]</code> in the garden.</p>

<p>Given an integer <code>n</code> and an integer array <code>ranges</code> of length <code>n + 1</code> where <code>ranges[i]</code> (0-indexed) means the <code>i-th</code> tap can water the area <code>[i - ranges[i], i + ranges[i]]</code> if it was open.</p>

<p>Return <em>the minimum number of taps</em> that should be open to water the whole garden, If the garden cannot be watered return <strong>-1</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png" style="width: 525px; height: 255px;" />
<pre>
<strong>Input:</strong> n = 5, ranges = [3,4,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
</pre>

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

<pre>
<strong>Input:</strong> n = 3, ranges = [0,0,0,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Even if you activate all the four taps you cannot water the whole garden.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>ranges.length == n + 1</code></li>
	<li><code>0 &lt;= ranges[i] &lt;= 100</code></li>
</ul>

from typing import List

class Solution:
    def minTaps(self, n: int, ranges: List[int]) -> int:
        # greedy intervals (minimum intervals to cover)
        # idea, add each new interval, find the overlapping interval
        # that reaches the furthest, continue.
        # O(nlogn) + O(n)
        intervals = []
        for i in range(n + 1):
            intervals.append( (max(0, i - ranges[i]), min(n, i + ranges[i])) )
        intervals.sort()
        end = furthest = used = i = 0
        while end < n:
            # of overlapping intervals with this LAST one, 
            # choose the furthest stretching one
            while i < n+1 and intervals[i][0] <= end:
                furthest = max(furthest, intervals[i][1])
                i+=1
            # the current interval started passed the end!
            # or we've exhausted all of the intervals and therefore we never 
            # executed the while statement of end == n
            if furthest == end:
                return -1            
            # we have successfully found the next one to carry one!
            used+=1
            end = furthest
        return used

        # DP for that other 'video stitching', note [1,4],[5,6] are 
        # NOT compatible (annoying, but that's usually the assumption) 
        # O(n^2), go by intervals in sorted order
        # Initialize DP array to infinity, meaning initially it's impossible to water any section
        dp = [float('inf')] * (n + 1)
        
        # will always be able to water, prove me wrong!?
        dp[0] = 0
        
        # don't have to worry about sorting in increasing order! Why?
        # for this particular problem, the ranges are anchored by 'i'
        # therefore by iterating i in INCREASING fashion, the intervals are sorted!
        for i in range(n + 1):
            lower_plant = max(0, i - ranges[i])
            upper_plant = min(n, i + ranges[i])
            
            # Update dp array for all positions this tap can cover
            for plant in range(lower_plant, upper_plant + 1):
                dp[plant] = min(dp[plant], dp[lower_plant] + 1)
        
        # If the end of the garden can't be covered, return -1
        return dp[n] if dp[n] != float('inf') else -1

18000