605. Can Place Flowers
Input: flowerbed = [1,0,0,0,1], n = 1
Output: trueInput: flowerbed = [1,0,0,0,1], n = 2
Output: false```python3
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
avail_slots = 0
for i in range(len(flowerbed)):
if flowerbed[i] == 0 and (i == 0 or flowerbed[i-1] == 0) and (i == len(flowerbed)-1 or flowerbed[i+1] == 0):
avail_slots += 1
flowerbed[i] = 1
if avail_slots >= n: #提早退出
return True
return avail_slots >= n
```Last updated