605 Can Place Flowers
Input:
flowerbed = [1,0,0,0,1], n = 1
Output:
TrueInput:
flowerbed = [1,0,0,0,1], n = 2
Output:
FalseLast updated
Input:
flowerbed = [1,0,0,0,1], n = 1
Output:
TrueInput:
flowerbed = [1,0,0,0,1], n = 2
Output:
FalseLast updated
n | opt
-------
1 | 0
2 | 0
3 | 1
4 | 1
5 | 2
6 | 2
7 | 3
. | .
. | .
. | .class Solution:
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
if len(flowerbed) is 1 and \
flowerbed[0] is 0:
return True
if len(flowerbed) >= 2 and \
flowerbed[0] is 0 and \
flowerbed[1] is 0:
flowerbed[0] = 1
n -= 1
if len(flowerbed) >= 3 and \
flowerbed[-1] is 0 and \
flowerbed[-2] is 0:
flowerbed[-1] = 1
n -= 1
consec_count = 1
for i in range(1, len(flowerbed)):
if flowerbed[i-1] is flowerbed[i]:
consec_count += 1
else:
n -= math.floor((consec_count - 1) / 2)
consec_count = 1
return n <= 0obj = Solution()
print(obj.canPlaceFlowers([1, 0, 0, 0, 1], 1)) # T
print(obj.canPlaceFlowers([1, 0, 0, 0, 1], 2)) # F
print(obj.canPlaceFlowers([1, 0, 0, 1, 0, 1], 1)) # F
print(obj.canPlaceFlowers([0, 0, 1, 0, 1, 1], 1)) # T
print(obj.canPlaceFlowers([0, 0, 0, 0, 0, 0], 3)) # T
print(obj.canPlaceFlowers([0, 0, 0, 0, 0, 0], 4)) # F