LeetCode 605. 种花问题(C、C++、python)

探讨在遵循特定规则下,如何判断是否能在给定的花坛中种植指定数量的花朵,而不违反相邻花朵不能同时存在的限制。

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

假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。

给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 。能否在不打破种植规则的情况下种入 朵花?能则返回True,不能则返回False。

示例 1:

输入: flowerbed = [1,0,0,0,1], n = 1
输出: True

示例 2:

输入: flowerbed = [1,0,0,0,1], n = 2
输出: False

注意:

数组内已种好的花不会违反种植规则。

输入的数组长度范围为 [1, 20000]。

n 是非负整数,且不会超过输入数组的大小。

C

bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) 
{
    if(n==0)
    {
        return true;
    }
    int m=flowerbedSize;
    int count=0;
    int* temp=(int*)malloc(sizeof(int)*((m+1)/2));
    int k=0;
    for(int i=0;i<m;i++)
    {
        if(flowerbed[i]==0)
        {
            count++;
        }
        else
        {
            if(count)
            {
                temp[k++]=count;
                count=0;
            }
        }
    }
    if(count==m)
    {
        return (m+1)/2>=n;
    }
    if(count)
    {
        temp[k++]=count;
    }
    if(k==0)
    {
        return false;
    }
    if(k==1)
    {
        if(flowerbed[0]==1 && flowerbed[m-1]==1)
        {
            return (temp[0]-1)/2>=n;
        }
        else
        {
            return temp[0]/2>=n;
        }
    }
    if(flowerbed[0]==0)
    {
        temp[0]/=2;
    }
    else
    {
        temp[0]=(temp[0]-1)/2;
    }
    if(flowerbed[m-1]==0)
    {
        temp[k-1]/=2;
    }
    else
    {
        temp[k-1]=(temp[k-1]-1)/2;
    }
    for(int i=1;i<k-1;i++)
    {
        temp[i]=(temp[i]-1)/2;
    }
    int sum=0;
    for(int i=0;i<k;i++)
    {
        sum+=temp[i];
    }
    return sum>=n;                                                                           
}

C++

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) 
    {
        int m=flowerbed.size();
        int res=1;
        vector<int> tmp;
        for(int i=1;i<m;i++)
        {
            if(0==flowerbed[i])
            {
                if(0==flowerbed[i-1])
                {
                    res++;                    
                }
                else
                {
                    res=1;
                }
            }
            else
            {
                if(0==flowerbed[i-1])
                {
                    tmp.push_back(res);
                }
            }
        }
        if(0==flowerbed[m-1])
        {
            tmp.push_back(res);
        }
        int ans=0;
        if(tmp.size() && m==tmp[0])
        {
            ans=(m+1)/2;
        }
        else
        {
            if(tmp.size())
            {
            for(auto it:tmp)
            {
                ans+=(it-1)/2;
            }
            if(0==flowerbed[0] && tmp[0]>1 && tmp[0]%2==0)
            {
                ans+=1;
            }
            if(0==flowerbed[m-1] && tmp.back()>1 && tmp.back()%2==0)
            {
                ans+=1;
            }                
            }

        }
        return ans>=n;
    }
};

python

class Solution:
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        if n==0:
            return True
        m=len(flowerbed)
        count=0
        temp=[]
        for i in range(0,m):
            if flowerbed[i]==0:
                count+=1
            else:
                if count:
                    temp.append(count)
                    count=0
        if count==m:
            return (count+1)//2>=n
        if count>0:
            temp.append(count)
        num=len(temp)
        if num==0:
            return False
        if num==1:
            if flowerbed[0]==1 and flowerbed[m-1]==1:
                return (temp[0]-1)//2>=n
            else:
                return temp[0]//2>=n
        if flowerbed[0]==0:
            temp[0]=temp[0]//2
        else:
            temp[0]=(temp[0]-1)//2
        if flowerbed[m-1]==0:
            temp[num-1]=temp[num-1]//2
        else:
            temp[num-1]=(temp[num-1]-1)//2
        for i in range(1,num-1):
            temp[i]=(temp[i]-1)//2
        return sum(temp)>=n

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值