var canPlaceFlowers = function(flowerbed, n) {
let data = [0, ...flowerbed]
data.push(0)
let count = 0,
i = 0
while (i < data.length - 1) {
if (data[i - 1] === 0 && data[i] === 0 && data[i + 1] === 0) {
count++
i += 2
} else {
i++
}
}
return count >= n
};
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
data = [0]+flowerbed+[0]
i = 1
count = 0
while i < len(data)-1:
if data[i-1] == 0 and data[i] == 0 and data[i+1] == 0:
count += 1
i += 2
else:
i += 1
return count >= n
func canPlaceFlowers(flowerbed []int, n int) bool {
count := 1
var result int
for i := 0; i < len(flowerbed); i++ {
if flowerbed[i] == 0 {
count++
} else {
result += (count - 1) / 2
count = 0
}
}
if count != 0 {
result += count / 2
}
return result >= n
}