class Solution:
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
def canPlaceFlowers(self, flowerbed, n):
i = sum = 0
if n == 0:
return True
if flowerbed[0] == 0:
flowerbed.insert(0, 0)
flowerbed.insert(0, 1)
if flowerbed[-1] == 0:
flowerbed.extend([0, 1])
for j in range(1, len(flowerbed)):
if flowerbed[j] == 1:
sum += (j - i) // 2 - 1
i = j
return sum >= n
本文介绍了一种解决花床种花问题的算法。该算法通过预处理输入数组并在遍历过程中计算可种植花朵的最大数量来判断是否能在指定的花床上种植n朵花。文章详细解释了算法的具体实现步骤。
376

被折叠的 条评论
为什么被折叠?



