1326. Minimum Number of Taps to Open to Water a Garden

There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).

There are n + 1 taps located at points [0, 1, ..., n] in the garden.

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

Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.

Example 1:

Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: 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]

Example 2:

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

Example 3:

Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3

Example 4:

Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2

Example 5:

Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1

Constraints:

  • 1 <= n <= 10^4
  • ranges.length == n + 1
  • 0 <= ranges[i] <= 100
class Solution {
    public int minTaps(int n, int[] ranges) {
        int l = n + 1;
        int[][] arr = new int[l][2];
        for(int i = 0; i < l; i++){
            arr[i][0] = i - ranges[i];
            arr[i][1] = i + ranges[i];
        }
        Arrays.sort(arr, (a, b)-> a[0] - b[0]);
        int res = videoStitching(arr, n);
        return res;
    }
        public int videoStitching(int[][] clips, int T) {
        int res = 0;
        for(int i = 0, st = 0, end = 0; st < T; res++, st = end){
            for(; i < clips.length && clips[i][0] <= st; i++){
                end = Math.max(end, clips[i][1]);
            }
            if(st == end) return -1;
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12227893.html

时间: 2024-08-01 01:09:24

1326. Minimum Number of Taps to Open to Water a Garden的相关文章

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Interval Minimum Number

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu

Reorder array to construct the minimum number

Construct minimum number by reordering a given non-negative integer array. Arrange them such that they form the minimum number. Notice The result may be very large, so you need to return a string instead of an integer. Have you met this question in a

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Leetcode: Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LeetCode] 452 Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs