Skip to content
Decoder

Can Place Flowers

Easy

Problem

A flowerbed is a row of plots: 1 is planted, 0 is empty. Flowers may not be planted in adjacent plots. The bed you are given already follows that rule. Return true if you can plant n new flowers without breaking it.

Example 1

Input
flowerbed = [1, 0, 0, 0, 1], n = 1
Output
true

Example 2

Input
flowerbed = [1, 0, 0, 0, 1], n = 2
Output
false

Example 3

Input
flowerbed = [0, 0, 1, 0, 0], n = 2
Output
true
Why
both ends

Constraints

  • 1 <= flowerbed.length <= 2 * 10^4
  • flowerbed[i] is 0 or 1.
  • No two adjacent plots are both 1.
  • 0 <= n <= flowerbed.length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.