java.lang.Object
g1301_1400.s1326_minimum_number_of_taps_to_open_to_water_a_garden.Solution

public class Solution extends Object
1326 - Minimum Number of Taps to Open to Water a Garden.<p>Hard</p> <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 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><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png" alt="" /></p> <p><strong>Input:</strong> n = 5, ranges = [3,4,1,1,0,0]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The tap at point 0 can cover the interval [-3,3]</p> <p>The tap at point 1 can cover the interval [-3,5]</p> <p>The tap at point 2 can cover the interval [1,3]</p> <p>The tap at point 3 can cover the interval [2,4]</p> <p>The tap at point 4 can cover the interval [4,4]</p> <p>The tap at point 5 can cover the interval [5,5]</p> <p>Opening Only the second tap will water the whole garden [0,5]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 3, ranges = [0,0,0,0]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> Even if you activate all the four taps you cannot water the whole garden.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>4</sup></code></li> <li><code>ranges.length == n + 1</code></li> <li><code>0 <= ranges[i] <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minTaps

      public int minTaps(int n, int[] ranges)