Created
August 9, 2023 13:16
-
-
Save kumrzz/0b4c7ca23634f281bb810092d2bafc06 to your computer and use it in GitHub Desktop.
leetcode 605 Can Place Flowers, mostly from walkccc.me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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