-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCanPlaceFlowers.js
39 lines (39 loc) · 923 Bytes
/
CanPlaceFlowers.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
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function(flowerbed, n) {
if(n === 0) {
return true;
}
if(flowerbed.length === 1) {
if(n > 1) {
return false;
}
else {
return flowerbed[0] === 0 ? true : false;
}
}
for(let i=0; i<flowerbed.length; i++) {
const plot = flowerbed[i];
if(i === 0) {
if(plot === 0 && flowerbed[i + 1] === 0) {
n--;
i++;
}
}
else if(i === flowerbed.length - 1) {
if(plot === 0 && flowerbed[i - 1] === 0) {
n--;
i++;
}
}
else if(plot === 0 && flowerbed[i + 1] === 0 && flowerbed[i - 1] === 0) {
n--;
i++;
}
if(n <= 0) return true;
}
return false;
};