Can Place Flowers

Easy Greedy

flowerbed is a row of plots, each 1 when a flower grows there and 0 when it is empty. No two flowers in it are in plots side by side.

New flowers may go only in empty plots, and the rule must still hold after planting: no flower may be in a plot next to another flower's, the new ones included.

Return true if all n new flowers can be planted, and false if not.

Example 1

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

The new flowers go in plots 0 and 4: each is empty, so are the plots beside it, and the two are not next to each other.

Example 2

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

Example 3

Input: flowerbed = [0,0,0,0,0], n = 3
Output: true

Constraints

  • 1 ≤ flowerbed.length ≤ 10⁵
  • flowerbed[i] is 0 or 1
  • No two 1s in flowerbed are side by side
  • 0 ≤ n ≤ flowerbed.length

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

More Greedy

  • Gas StationAI round · Medium
  • Largest NumberAI round · Medium
  • CandyAI round · Hard
  • Increasing Triplet SubsequenceMedium
  • Minimum Number of Arrows to Burst BalloonsMedium
All Greedy problems