code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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