Skip to content

Instantly share code, notes, and snippets.

@kuntalchandra
Created December 5, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuntalchandra/b5729b1a5452a12df14b63b08373fb45 to your computer and use it in GitHub Desktop.
Save kuntalchandra/b5729b1a5452a12df14b63b08373fb45 to your computer and use it in GitHub Desktop.
Can Place Flowers
"""
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 if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
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
"""
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
flowerbed = [0] + flowerbed + [0]
counter = 0
for i in range(1, len(flowerbed) - 1):
if flowerbed[i]:
continue
if not flowerbed[i - 1] + flowerbed[i + 1]:
flowerbed[i] = 1
counter += 1
return counter >= n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment