当前位置: 首页 > news >正文

接雨水 - 困难

*************

C++

topic: 42. 接雨水 - 力扣(LeetCode)

*************

Give the topic an inspection.

Ley's try something hard. before solve this problem, I still begin my 涩话. In fact, I go to the concert in Zhenjiang, the singer is Xusong. I listen his song since my middle school. At the end of the last class in the moring, 玫瑰花的葬礼 reminded us 'go for lunch, kids'. And once I heard this song and I will be hungary for a short time. Middle school age is a beautiful time. I am aware that time is limited. I should pay more time to experience somrthing reallly fun. If I can be right back to my middle school, I will tell that little kid 'hey kid, donot wear that fucking glasses, that's not cool.'

Back to the topic and try something kind of hard. The key point is thinking like a computer. Iterate through all the array is possible. So think about it, caculate each position which can drink how many glasses of rains and summate them.

For position[i], find the tallest one on the left and the tallest one on the right, the glasses of rains is min(max_left, max_right) - position[i[.

then the code can start.

class Solution {
public:int trap(vector<int>& height) {int n      = height.size();int result = 0;for (int i = 1; i < n - 1; i++){// find the max value on the left// find the max value on the right// result = result + current_result}}
};

find the max value on the left and right.

        // 遍历每个柱子(除了第一个和最后一个,因为它们无法形成积水)for (int i = 1; i < n - 1; i++) {int left_max = 0; // 初始化左边的最大高度int right_max = 0; // 初始化右边的最大高度// 找到当前柱子左边的最大高度for (int j = i - 1; j >= 0; j--) {left_max = max(left_max, height[j]);}// 找到当前柱子右边的最大高度for (int j = i + 1; j < n; j++) {right_max = max(right_max, height[j]);}

summate the result.

            int current_result = min(left_max, right_max) - height[i];if (current_result > 0) {result += current_result;

and the total code is as follow.

class Solution {
public:int trap(vector<int>& height) {int n      = height.size();int result = 0;for (int i = 1; i < n - 1; i++){// find the max value on the leftint left_max  = 0;int right_max = 0;// 找到当前柱子左边的最大高度for (int j = i - 1; j >= 0; j--) {left_max = max(left_max, height[j]);}// 找到当前柱子右边的最大高度for (int j = i + 1; j < n; j++) {right_max = max(right_max, height[j]);}// 当前柱子能接的雨水量等于 min(left_max, right_max) - height[i]// 如果这个值小于0,说明当前柱子无法接水,直接跳过int current_result = min(left_max, right_max) - height[i];if (current_result > 0) {result += current_result;}// find the max value on the right// result = result + current_result}return result;}
};

This is not my first time to do this project and I still cannot write the code at once. Need more practice. But the good news is that I have the concept to solve the problem.

http://www.lryc.cn/news/571242.html

相关文章:

  • Java 常用类 Time API:现代时间处理的艺术
  • GPU算力应用迈出关键一步:DPIN与南洋生物科技合作落地
  • 如何设置端口映射? 常见本地计算机内网ip端口映射给公网外网访问的详细方法步骤
  • 深入剖析Spring Cloud Gateway,自定义过滤器+断言组合成拦截器链实现Token认证
  • Win32 专栏停更公告
  • 讲透 RNN 到 Transformer !!!
  • k8s 收集event事件至Loki
  • Kafka 简介(附电子教程资料)
  • 云计算-Raft算法报告-raft与paxos对比
  • 【MySQL基础】表的功能实现:增删查改详细讲解
  • 第十七届山东省职业院校技能大赛中职组网络建设与运维赛项
  • php在线生成pdf选民证系统支持中文(小工具)
  • 【前端基础】摩天之建的艺术:html(下)
  • 数据库的查询
  • 游戏技能编辑器开发完全指南系统架构设计之技能编辑器整体架构
  • RISC-V向量扩展与GPU协处理:开源加速器设计新范式——对比NVDLA与香山架构的指令集融合方案
  • 【开源工具】Windows屏幕控制大师:息屏+亮度调节+快捷键一体化解决方案
  • 数字化零售如何全面优化顾客体验
  • 【SpringBoot】Spring Boot实现SSE实时推送实战
  • TDMQ CKafka 版事务:分布式环境下的消息一致性保障
  • 工业视觉应用开发教程(一)
  • KingbaseES在线体验平台:开启国产数据库学习新征程
  • Mybatis(XML映射文件、动态SQL)
  • 有趣的git
  • 机器学习项目微服务离线移植
  • 洪水风险图制作全流程:HEC-RAS 与 ArcGIS 的耦合应用
  • Rocky Linux 9 系统初始化与安全加固脚本
  • MySQL的Sql优化经验总结
  • 大模型知识库RAG框架,比如LangChain、ChatChat、FastGPT等等,哪个效果比较好
  • 执行 PGPT_PROFILES=ollama make run下面报错,