Open Soohan-Kim opened 5 months ago
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
slots = 0
if len(flowerbed) == 1:
if n:
if flowerbed[0]:
return False
else:
return True
else:
return True
cur = sum(flowerbed)
if flowerbed[0]:
slots += 1
elif len(flowerbed) > 1 and flowerbed[1] == 0:
slots += 1
flowerbed[0] = 1
if flowerbed[-1]:
slots += 1
elif len(flowerbed) > 1 and flowerbed[-2] == 0:
slots += 1
flowerbed[-1] = 1
for i in range(1, len(flowerbed)-1):
if flowerbed[i] == 0:
if flowerbed[i-1] == 0 and flowerbed[i+1] == 0:
slots += 1
flowerbed[i] = 1
else:
slots += 1
if n <= slots - cur:
return True
return False
https://leetcode.com/problems/can-place-flowers/description/