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

news/2024/9/29 17:51:40/

题目:

解题思路:

        面对每一株植物有两种情况 水够 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;
}


http://www.ppmy.cn/news/1532024.html

相关文章

每日一题|2073. 买票需要的时间|循环数组、下标分类

本题的数据范围比较大&#xff0c;如果是直接模拟的话会超时。 所以&#xff0c;一个更直接的想法是&#xff0c;在第k个人买完时&#xff0c;每个人分别都花了多少时间&#xff1f; 也就是&#xff0c;求出买票循环结束前每个人所花的时间。 那么&#xff0c;对于k之前的人…

Qualcomm AI Hub模型优化1: Whisper-Base-En导出及问题解决

1 从Qualcomm AI Hub Module中选择Whisper-Base-En模块部署 1.1 进入module虚拟环境 python3 -m venv qai_hub_models_env && source qai_hub_models_env/bin/activate1.2 使用pip安装高通音频转录包 pip install "qai_hub_models[whisper_base_en]" 1.3…

Solidity智能合约中的异常处理(error、require 和 assert)

Solidity 中的三种抛出异常方法&#xff1a;error、require 和 assert 在 Solidity 开发中&#xff0c;异常处理是确保智能合约安全性和正确性的关键步骤。Solidity 提供了三种主要方法来抛出异常&#xff1a;error、require 和 assert。本文将详细介绍这三种方法的用途、实现方…

正则表达式在过滤交换机lldp信息的应用举例

#include <iostream> #include <string> #include <regex> #include <vector> #include <unordered_map> #include <sstream> #include <unistd.h> // For usleep// 假设存在的 LOG_INFO 和 LOG_WARNING 函数 #define LOG_INFO(...)…

SpringMVC4-SpringMVC获取请求参数

目录 通过ServletAPI获取&#xff08;不常用&#xff09; 通过控制器方法的形参获取请求参数 RequestParam RequestHeader CookieValue 通过POJO获取请求参数 解决获取请求参数的乱码问题 test_param.html&#xff1a; <!DOCTYPE html> <html lang"en&qu…

性能调优知识点(mysql)三

SQL底层执行原理 MySQL的内部组件结构&#xff1a;大体来说&#xff0c;MySQL 可以分为 Server 层和存储引擎层store两部分 Server层:主要包括连接器、查询缓存、分析器、优化器、执行器等&#xff0c;涵盖 MySQL 的大多数核心服务功能&#xff0c;以及所有的内置函数&#xf…

R包:VennDiagram韦恩图

加载R包 library(VennDiagram)数据 # Prepare character vectors v1 <- c("DKK1", "NPC1", "NAPG", "ERG", "VHL", "BTD", "MALL", "HAUS1") v2 <- c("SMAD4", "DKK1…

一、Spring Boot集成Spring Security之自动装配

Spring Boot集成Spring Security之自动装配介绍 一、实现功能及软件版本说明二、创建Spring Boot项目三、查看自动装配配置类四、自动装配配置类之SecurityAutoConfiguration1、SecurityAutoConfiguration部分源码2、主要作用3、SpringBootWebSecurityConfiguration3.1、Spring…