-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1326-minimum-number-of-taps-to-open-to-water-a-garden.js
49 lines (42 loc) · 1.36 KB
/
1326-minimum-number-of-taps-to-open-to-water-a-garden.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 1326. Minimum Number of Taps to Open to Water a Garden
* https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number} n
* @param {number[]} ranges
* @return {number}
*/
var minTaps = function(n, ranges) {
const intervals = ranges.map((range, index) => [
Math.max(0, index - range),
Math.min(n, index + range)
]);
intervals.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
let taps = 0;
let currentEnd = 0;
let nextEnd = 0;
let i = 0;
while (i < intervals.length && currentEnd < n) {
taps++;
while (i < intervals.length && intervals[i][0] <= currentEnd) {
nextEnd = Math.max(nextEnd, intervals[i][1]);
i++;
}
if (currentEnd === nextEnd) return -1;
currentEnd = nextEnd;
}
return currentEnd >= n ? taps : -1;
};