code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

605 Can Place Flowers

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const canPlaceFlowers = (flowerbed, n) => {
  const newBed = [0, ...flowerbed, 0]
  for (let i = 1; i < newBed.length - 1; i++) {
    if (newBed[i - 1] === 0 && newBed[i] === 0 && newBed[i + 1] === 0) {
      n = n - 1
      newBed[i] = 1
    }
  }
  return n === 0
}

console.log(canPlaceFlowers([0, 0, 1, 0, 0], 1))

Created 2022-03-21T09:33:01+00:00 · Edit