【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;
    }

相关推荐
小魏每天都学习9 分钟前
【算法——c/c++]
c语言·c++·算法
智码未来学堂43 分钟前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn1 小时前
基于封装的专项 知识点
java·前端·python·算法
春日见1 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶
副露のmagic1 小时前
更弱智的算法学习 day59
算法
u0109272712 小时前
C++中的RAII技术深入
开发语言·c++·算法
彷徨而立3 小时前
【C/C++】strerror、GetLastError 和 errno 的含义和区别?
c语言·c++
2401_832131953 小时前
模板错误消息优化
开发语言·c++·算法
金枪不摆鳍3 小时前
算法--二叉搜索树
数据结构·c++·算法
近津薪荼3 小时前
优选算法——双指针6(单调性)
c++·学习·算法