> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Can Place Flowers Python Solution with Tests

> Tested Python solution for LeetCode 605 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 605, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/can-place-flowers/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 605   # by problem number
lcpy gen -s can_place_flowers   # by problem name
```

## Problem

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots.

Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return `true` *if* `n` *new flowers can be planted in the* `flowerbed` *without violating the no-adjacent-flowers rule and* `false` *otherwise*.

### Examples

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

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

### Constraints

* 1 \<= flowerbed.length \<= 2 \* 10^4
* flowerbed\[i] is 0 or 1.
* There are no two adjacent flowers in flowerbed.
* 0 \<= n \<= flowerbed.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/can_place_flowers/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/can_place_flowers/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m) where m = len(flowerbed)
    # Space: O(m)
    def can_place_flowers(self, flowerbed: list[int], n: int) -> bool:
        planted = 0
        bed = flowerbed[:]
        for i, plot in enumerate(bed):
            left_empty = i == 0 or bed[i - 1] == 0
            right_empty = i == len(bed) - 1 or bed[i + 1] == 0
            if plot == 0 and left_empty and right_empty:
                bed[i] = 1
                planted += 1
        return planted >= n
```

## Complexity

| Time | Space |
| - | - |
| O(m) where m = len(flowerbed) | O(m) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.