Skip to content

Commit 27b66db

Browse files
authored
Create 1326-minimum-number-of-taps-to-open-to-water-a-garden.js
1 parent 03a3932 commit 27b66db

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[]} ranges
4+
* @return {number}
5+
*/
6+
const minTaps = function (n, ranges) {
7+
const starts = new Array(n + 1).fill(0)
8+
for (let i = 0; i <= n; i++) {
9+
const start = Math.max(0, i - ranges[i])
10+
starts[start] = Math.max(starts[start], i + ranges[i])
11+
}
12+
let count = 0
13+
let max = 0
14+
let i = 0
15+
while (max < n) {
16+
const end = max
17+
for (let j = i; j <= end; j++) {
18+
max = Math.max(max, starts[j])
19+
}
20+
if (i === max) return -1
21+
i = end
22+
count++
23+
}
24+
return count
25+
}

0 commit comments

Comments
 (0)