""" 0605.1 - Can Place Flowers - Solution 1 - Greedy Scan """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
"""Can Place Flowers Function"""
for i in range(len(flowerbed)):
if flowerbed[i] == 0:
left_empty = (i == 0) or (flowerbed[i - 1] == 0)
right_empty = (i == len(flowerbed) - 1) or (flowerbed[i + 1] == 0)
if left_empty and right_empty:
flowerbed[i] = 1
n -= 1
if n <= 0:
return True
return n <= 0
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().canPlaceFlowers([1, 0, 0, 0, 1], 1)) # True
print(Solution().canPlaceFlowers([1, 0, 0, 0, 1], 2)) # False
print(Solution().canPlaceFlowers([0, 0, 1, 0, 0], 2)) # True
print(Solution().canPlaceFlowers([1], 0)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0605.2 - Can Place Flowers - Solution 2 - Counting Consecutive Zeros """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
"""Can Place Flowers Function"""
count = 0
zeros = 1 # treat left boundary as an implicit zero
for plot in flowerbed:
if plot == 0:
zeros += 1
else:
count += (zeros - 1) // 2
zeros = 0
zeros += 1 # treat right boundary as an implicit zero
count += (zeros - 1) // 2
return count >= n
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().canPlaceFlowers([1, 0, 0, 0, 1], 1)) # True
print(Solution().canPlaceFlowers([1, 0, 0, 0, 1], 2)) # False
print(Solution().canPlaceFlowers([0, 0, 1, 0, 0], 2)) # True
print(Solution().canPlaceFlowers([1], 0)) # True
print(Solution().canPlaceFlowers([0], 1)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()