Minimum Number of Buckets Required to Collect Rainwater from Houses LeetCode solution in JavaScript

1
var minimumBuckets = function (street) {
2
const n = street.length;
3
const chars = street.split("");
4

5
let count = 0;
6

7
for (let i = 0; i < n; ++i) {
8
const char = chars[i];
9

10
if (char === "H") {
11
if (i > 0 && chars[i - 1] === "B") {
12
continue;
13
} else if (i < n - 1 && chars[i + 1] === ".") {
14
++count;
15
chars[i + 1] = "B";
16
} else if (i > 0 && chars[i - 1] === ".") {
17
++count;
18
chars[i - 1] = "B";
19
} else {
20
return -1;
21
}
22
}
23
}
24

25
return count;
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0