Show problems as
Classic problem #605EasyArrayGreedy

Can Place Flowers

Real-world version: Blade Spacing in the Rack
Tracked in this browser only
Read Can Place Flowers on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A server rack is one straight row of slots, given to you as the array slots. An entry of 1 means that slot already carries a blade and an entry of 0 means the slot is bare.

Airflow rules forbid two blades in slots that touch, and the rack as handed over already respects that, so no two 1 entries sit side by side.

The data centre wants extra further blades racked. A blade may go into a bare slot only when the slots touching it are bare as well; a slot at either end of the row has just one neighbour to satisfy. As soon as a blade goes in, that slot counts as carrying a blade for everything installed afterwards.

Return true when all extra blades can be racked and false when they cannot. Racking only some of them does not count as success. When extra is 0 there is nothing to install and the answer is true.

Examples

Example 1

Input
slots = [0, 0, 1, 0, 0, 0], extra = 2
Output
true

One blade fits in slot 0, which blocks slot 1, and slot 3 is already blocked by the blade in slot 2. Slot 4 is still free on both sides, so the second blade fits there.

Example 2

Input
slots = [0, 0, 1, 0, 0, 0], extra = 3
Output
false

The same rack has room for two blades at most, so a request for three cannot be met.

Example 3

Input
slots = [0], extra = 1
Output
true

A row of one bare slot has no neighbouring slot at all, so nothing blocks the single blade.

Constraints

  • 1 <= slots.length <= 2 * 10^4
  • slots[i] is 0 or 1.
  • No two slots carrying a blade touch each other in the rack as given.
  • 0 <= extra <= slots.length

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def blade_spacing(slots: list[int], extra: int) -> bool:
Java
public boolean bladeSpacing(int[] slots, int extra)