Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c17e82c commit c1b8688Copy full SHA for c1b8688
javascript/605-can-place-flowers.js
@@ -0,0 +1,19 @@
1
+// time complexity is O(n).
2
+
3
+var canPlaceFlowers = function(flowerbed, n) {
4
5
+ for(let i = 0; i < flowerbed.length; i++) {
6
+ if(flowerbed[i] === 0) {
7
+ if((flowerbed[i-1] === 0 && flowerbed[i+1] === 0) ||
8
+ (flowerbed[i-1] === undefined && flowerbed[i+1] === 0) ||
9
+ (flowerbed[i+1] === undefined && flowerbed[i-1] === 0) ||
10
+ (flowerbed[i-1] === undefined && flowerbed[i+1] === undefined && flowerbed[i] === 0)) {
11
12
+ flowerbed[i] = 1;
13
+ n--;
14
+ }
15
16
17
18
+ return n > 0 ? false : true;
19
+};
0 commit comments