[Swift]LeetCode1326. 灌溉花園的最少水龍頭數目 | Minimum Number of Taps to Open to Water a Garden


★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公眾號:山青詠芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/12213419.html
➤如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

熱烈歡迎,請直接點擊!!!

進入博主App Store主頁,下載使用各個作品!!!

注:博主將堅持每月上線一個新app!!!

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


在 x 軸上有一個一維的花園。花園長度為 n,從點 0 開始,到點 n 結束。

花園里總共有 n + 1 個水龍頭,分別位於 [0, 1, ..., n] 。

給你一個整數 n 和一個長度為 n + 1 的整數數組 ranges ,其中 ranges[i] (下標從 0 開始)表示:如果打開點 i 處的水龍頭,可以灌溉的區域為 [i -  ranges[i], i + ranges[i]] 。

請你返回可以灌溉整個花園的 最少水龍頭數目 。如果花園始終存在無法灌溉到的地方,請你返回 -1 。

 

示例 1:

 

 

 

輸入:n = 5, ranges = [3,4,1,1,0,0]
輸出:1
解釋:
點 0 處的水龍頭可以灌溉區間 [-3,3]
點 1 處的水龍頭可以灌溉區間 [-3,5]
點 2 處的水龍頭可以灌溉區間 [1,3]
點 3 處的水龍頭可以灌溉區間 [2,4]
點 4 處的水龍頭可以灌溉區間 [4,4]
點 5 處的水龍頭可以灌溉區間 [5,5]
只需要打開點 1 處的水龍頭即可灌溉整個花園 [0,5] 。
示例 2:

輸入:n = 3, ranges = [0,0,0,0]
輸出:-1
解釋:即使打開所有水龍頭,你也無法灌溉整個花園。
示例 3:

輸入:n = 7, ranges = [1,2,1,0,2,1,0,1]
輸出:3
示例 4:

輸入:n = 8, ranges = [4,0,0,0,0,0,0,0,4]
輸出:2
示例 5:

輸入:n = 8, ranges = [4,0,0,0,4,0,0,0,4]
輸出:1
 

提示:

1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100


Runtime: 148 ms
Memory Usage: 21.7 MB
 1 class Solution {
 2     func minTaps(_ n: Int, _ ranges: [Int]) -> Int {
 3         var rg:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: 2), count: n + 1)
 4         for i in 0...n
 5         {
 6             let s:Int =  i - ranges[i]
 7             let e:Int =  i + ranges[i]
 8             rg[i] = [s < 0 ? 0 : s, e < n ? e : n]
 9         }
10         rg = rg.sorted(by:{
11             if $0[0] == $1[0]
12             {
13                 return $0[1] < $1[1]
14             }
15             else
16             {
17                 return $0[0] < $1[0]
18             }
19         }
20         )
21         var ans:Int = 0
22         var i:Int = 0
23         var start:Int = 0
24         var end:Int = 0
25         while(start < n && i <= n)
26         {
27             while(i <= n && rg[i][0] <= start)
28             {
29                 end = max(end, rg[i][1])
30                 i += 1
31             }
32             if end <= start{return -1}
33             start = end
34             ans += 1
35         }
36         return ans
37     }
38 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM