Skip to content

Commit 5738ba8

Browse files
authored
Create 605-can-place-flowers.js
1 parent d646213 commit 5738ba8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

605-can-place-flowers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} flowerbed
3+
* @param {number} n
4+
* @return {boolean}
5+
*/
6+
const canPlaceFlowers = function(flowerbed, n) {
7+
let count = 0
8+
const clone = flowerbed
9+
for(let i = 0; i < clone.length; i++) {
10+
if(clone[i] === 0) {
11+
if(i === 0 && (clone[i + 1] === 0 || clone[i+1] == null)){
12+
count++
13+
clone[i] = 1
14+
}
15+
if(i > 0 && i < clone.length - 1 && clone[i - 1] === 0 && clone[i + 1] === 0) {
16+
count++
17+
clone[i] = 1
18+
}
19+
if(i === clone.length - 1 && clone[i - 1] === 0 && clone[i] === 0) {
20+
count++
21+
clone[i] = 1
22+
}
23+
}
24+
}
25+
26+
return count >= n ? true : false
27+
};

0 commit comments

Comments
 (0)