LeetCode605. Can Place Flowers

本文介绍了一种解决花床种花问题的算法。该算法通过预处理输入数组并在遍历过程中计算可种植花朵的最大数量来判断是否能在指定的花床上种植n朵花。文章详细解释了算法的具体实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值