【C语言刷力扣】2079.给植物浇水

题目:

解题思路:

面对每一株植物有两种情况水够 or 水不够:

  • 水够: result加1即向前走一步

  • 水不够: 走回河边再走回来并向前走一步,走到下一植物 result += 2 * i + 1

    int wateringPlants(int* plants, int plantsSize, int capacity) {
    int result = 0;
    int water = capacity;
    for (int i = 0; i < plantsSize; i++) {
    water -= plants[i];
    if (water >= 0) {
    result++;
    }
    else {
    result += 2 * i + 1;
    water = capacity - plants[i];
    }
    }
    return result;
    }

相关推荐
石一峰69915 分钟前
C 语言函数设计模式实战经验
c语言·开发语言·设计模式
happymaker062628 分钟前
LeetCodeHot100——155.最小栈
算法
洛水水39 分钟前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Coder-magician43 分钟前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Kurisu_红莉栖43 分钟前
力扣56合并区间
算法·leetcode
Irissgwe1 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
随意起个昵称1 小时前
区间dp-基础题目3(永别)
c++·算法
周末也要写八哥1 小时前
有向图Hierholzer算法的另一种实现
算法
bIo7lyA8v1 小时前
算法调优中的性能回归与基准测试分析的技术8
算法·数据挖掘·回归
有点。1 小时前
C++贪心算法二(练习题)
c++·算法·贪心算法