Skip to content

Instantly share code, notes, and snippets.

@kumrzz
Created August 9, 2023 13:16
Show Gist options
  • Save kumrzz/0b4c7ca23634f281bb810092d2bafc06 to your computer and use it in GitHub Desktop.
Save kumrzz/0b4c7ca23634f281bb810092d2bafc06 to your computer and use it in GitHub Desktop.
leetcode 605 Can Place Flowers, mostly from walkccc.me
class Solution:
def canPlaceFlowers(self, flowerbed, n) -> bool:
for i, flower in enumerate(flowerbed):
if flower == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0):
flowerbed[i] = 1
n -= 1
if n <= 0:
return True
return False
# answer 1
flowerbed = [1,0,0,0,1]
n=1
print(Solution.canPlaceFlowers(1,flowerbed,n))
# answer 2
flowerbed = [1,0,0,0,1]
n = 2
print(Solution.canPlaceFlowers(1,flowerbed,n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment