Skip to content

605. Can Place Flowers

Difficulty: EASY

function canPlaceFlowers(flowerbed: number[], n: number): boolean {
let noAdjacent = 0;
flowerbed.forEach((_value, index) => {
let prev = flowerbed[index - 1];
let next = flowerbed[index + 1];
if (!prev && !flowerbed[index] && !next) {
noAdjacent++;
flowerbed[index] = 1;
}
});
return noAdjacent >= n;
}
console.log(canPlaceFlowers([1, 0, 0, 0, 1], 1));
console.log(canPlaceFlowers([1, 0, 0, 0, 1], 2));
console.log(canPlaceFlowers([0, 0, 1, 0, 1], 1));
console.log(canPlaceFlowers([1, 0], 1));