当前位置:网站首页>【刷题篇】接雨水(一维)
【刷题篇】接雨水(一维)
2022-07-03 03:52:00 【m0_60631323】
一、题目
OJ链接
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
二、题解
2.1思路


2.2源码
public int trap(int[] height) {
if(height==null||height.length<2){
return 0;
}
int N=height.length;
int leftMax=height[0];
int rightMax=height[N-1];
int L=1;
int R=N-2;
int water=0;
while(L<=R){
if(leftMax<=rightMax){
water+=Math.max(0,leftMax-height[L]);
leftMax=Math.max(leftMax,height[L++]);
}else {
water+=Math.max(0,rightMax-height[R]);
rightMax=Math.max(rightMax,height[R--]);
}
}
return water;
}
边栏推荐
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
- For instruction, uploading pictures and display effect optimization of simple wechat applet development
- 简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
- Summary of electromagnetic spectrum
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- 8.8.2-PointersOnC-20220214
- ffmpeg下载安装教程及介绍
- Docker install and start MySQL service
- Role of JS No
- [set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
猜你喜欢

The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units

2022 P cylinder filling examination content and P cylinder filling practice examination video

2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
![[embedded module] OLED display module](/img/c4/474f5ee580d132654fbd1a4cd53bab.jpg)
[embedded module] OLED display module

释放数据力量的Ceph-尚文网络xUP楠哥

Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)

ffmpeg录制屏幕和截屏

递归:深度优先搜索

How does the pytorch project run?

FileZilla Client下載安裝
随机推荐
Separable bonds and convertible bonds
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
What can learning pytorch do?
MongoDB简介
Summary of electromagnetic spectrum
Error in compiled file: error: unmapped character encoding GBK
简易版 微信小程序开发之for指令、上传图片及展示效果优化
递归:快速排序,归并排序和堆排序
动态规划:最长回文子串和子序列
FileZilla Client下载安装
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
Web session management security issues
leetcode:297. 二叉树的序列化与反序列化
MongoDB安装 & 部署
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Mongodb replication set [master-slave replication]
Arlo's thinking about himself
Recursion: depth first search