605. 种花问题003(贪心算法+思路+详解)

一:题目

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

给你一个整数数组 flowerbed 表示花坛,由若干 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

二:思路

从左向右遍历花坛,在可以种花的地方就种一朵,能种就种
(因为在任一种花时候,不种都不会得到更优解),就是一种贪心的思想
这里可以种花的条件是:
自己为空
自己是最左 或者 左边为空
自己是最右 或者 右边为空

i == 0 ||flowerbed[i-1] == 0 :这里的顺序不能改变 因为当 i==0时
如果flowerbed[i-1]在前面就会出现越界问题

三:上码

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {

     /**
        从左向右遍历花坛,在可以种花的地方就种一朵,能种就种
        (因为在任一种花时候,不种都不会得到更优解),就是一种贪心的思想
        这里可以种花的条件是:
            自己为空
            自己是最左 或者 左边为空 
            自己是最右 或者 右边为空

            i == 0 ||flowerbed[i-1] == 0 :这里的顺序不能改变 因为当 i==0时 
            如果flowerbed[i-1]在前面就会出现越界问题 

     */   

    int count = 0;

    for(int i = 0; i < flowerbed.size(); i++){
        
        if(flowerbed[i] == 0 
                && ( i == 0 ||flowerbed[i-1] == 0 ) 
                && (i + 1 == flowerbed.size() || flowerbed[i+1] == 0  )){
                    flowerbed[i] = 1;
                    count++;
                }
    }

    if(count >= n)
        return true;
    else
        return false; 

    }
};
在Python中,贪心算法是一种通过每一步局部最优选择来达到全局最优解的问题求解策略。对于种花问题,我们可以假设我们有不同种类的花,每种花需要一定的土壤、阳光和水分才能生长,并且每种花的价值也不同。贪心算法在这种问题中可能会假设每次选择当前价值最高的花,直到资源(如土壤、阳光和水分)耗尽。 下面是一个简单的贪心算法示例,这里假设我们有一个函数`get_value(plant, resources)`,它返回植物在给定资源下的价值,以及一个函数`check_resources(resource_needed, available_resources)`检查是否满足种植需求: ```python def greedy_flower_planting(profit_per_flower, soil_capacity, sunlight_capacity, water_capacity): flowers = sorted(profit_per_flower.items(), key=lambda x: x[1], reverse=True) # 按价值降序排序 total_profit = 0 soil_left = soil_capacity sunlight_left = sunlight_capacity water_left = water_capacity for flower, profit in flowers: resource_needed = (flower.soil, flower.sunlight, flower.water) if check_resources(resource_needed, [soil_left, sunlight_left, water_left]): total_profit += profit soil_left -= flower.soil sunlight_left -= flower.sunlight water_left -= flower.water else: break # 如果资源不够,就不再种植此花 return total_profit # 示例数据结构,每个flower字典包含值(profit)、土壤需求、阳光需求和水分需求 flowers = [ {"name": "A", "profit": 10, "soil": 5, "sunlight": 4, "water": 3}, {"name": "B", "profit": 8, "soil": 7, "sunlight": 3, "water": 2}, {"name": "C", "profit": 6, "soil": 4, "sunlight": 5, "water": 1} ] # 调用算法 total_profit = greedy_flower_planting(profit_per_flower={flower["name"]: flower["profit"] for flower in flowers}, soil_capacity=100, sunlight_capacity=100, water_capacity=100) print(f"最大利润:{total_profit}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天向上的菜鸡杰!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值