leetcode题:605. 种花问题(简单)一开始没做出来

本文探讨了种花问题的算法解决方案,即如何在遵循特定规则的情况下,在花坛中种植最多数量的花朵。通过遍历数组并应用逻辑判断,文章提供了一种有效的方法来确定是否能在给定条件下种下所需数量的花。

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

一、题目描述:605. 种花问题(简单)

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

给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回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 是非负整数,且不会超过输入数组的大小。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/can-place-flowers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、解题思路

遍历flowerbed[]数组,依次判断各位置是否适合种花,判断的标准是当前位置为0,且前一位置为0或其下标为-1,且后一位置为0或其下标为flowerbed.length。若判断为真,则修改flowerbed[]数组当前位置为1,n--。最后若n<=0,则能种下所有花

三、代码

public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed[i] == 0 
            && (i - 1 == -1 || flowerbed[i - 1] == 0) 
            && (i + 1 == flowerbed.length || flowerbed[i + 1] == 0)) {
                n--;
                flowerbed[i] = 1;
            }
        }
        return n <= 0;
    }

递归方式

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        map<int,int> save;
        int count = canPlaceFlowers(flowerbed,0,save);
        //cout<<"count="<<count<<endl;
        return count >= n;
    }
    int canPlaceFlowers(vector<int>& flowerbe,int index,map<int,int> & save)
    {
        if(flowerbe.size() <= index)
            return 0;
        if(flowerbe.size() == 1 && flowerbe[index] == 0)
            return 1;
        //if(save.count(index) > 0)
         //   return save[index];
        while((flowerbe.size() > index + 1 && flowerbe[index] == 0 && flowerbe[index+1] == 1) || (flowerbe.size() > index+1 && flowerbe[index] == 1 && flowerbe[index+1] == 0) || (flowerbe.size() > index + 1 && flowerbe[index] == 1 && flowerbe[index+1] == 1))
        {
            //cout<<"indexpass="<<index<<endl;
            if(flowerbe[index] == 1 && flowerbe[index+1] == 1)
            {
                
                index++;
            }
            else if(flowerbe[index] == 1 && flowerbe[index+1] == 0)
            {
                index+=2;

            }
            else
            {
                index+=3;
            }
            
        }
        if(index >= flowerbe.size())
            return 0;
        if(flowerbe.size() == index + 1 && flowerbe[index] == 0)
            return 1;
        if(flowerbe.size() == index + 1 && flowerbe[index] == 1)
            return 0;
        int add = 0;
        if(flowerbe.size() >=2)
            add = canPlaceFlowers(flowerbe,index + 2,save);
        //cout<<"index="<<index<<endl;
        //cout<<"add="<<add<<endl;
       // int next = 0;
        //if(flowerbe.size() >=1)
        //    next = canPlaceFlowers(flowerbe,index+1,save);
        //cout<<"next="<<next<<endl;
       // int count_all = max(add+1,next);
       // save[index] = count_all;
 
        return add + 1;
    };
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值