当前位置:网站首页>【刷题篇】接雨水(一维)
【刷题篇】接雨水(一维)
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;
}
边栏推荐
- 释放数据力量的Ceph-尚文网络xUP楠哥
- Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
- Ansible简介【暂未完成(半成品)】
- ffmpeg之 一张/多张图片合成视频
- How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
- [mathematical logic] predicate logic (first-order predicate logic formula | example)
- 递归:一维链表和数组
- @The difference between Autowired, @qualifier, @resource
- 【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
- 简易版 微信小程序开发之for指令、上传图片及展示效果优化
猜你喜欢

MySQL MAC download and installation tutorial

Ffmpeg download and installation tutorial and introduction

Mysql Mac版下载安装教程

Hutool dynamically adds scheduled tasks

pytorch是什么?pytorch是一个软件吗?
![[MySQL] the difference between left join, right join and join](/img/d4/8684cd59cd1bd77e70bd4d7c7074c3.jpg)
[MySQL] the difference between left join, right join and join
![[embedded module] OLED display module](/img/c4/474f5ee580d132654fbd1a4cd53bab.jpg)
[embedded module] OLED display module

Error in compiled file: error: unmapped character encoding GBK

105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode

pytorch项目怎么跑?
随机推荐
阿洛对自己的思考
可分离债券与可转债
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
Error in compiled file: error: unmapped character encoding GBK
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
8.8.2-PointersOnC-20220214
pytorch开源吗?
numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
Tidal characteristics of the Bohai Sea and the Yellow Sea
MongoDB主配置文件
2022 P cylinder filling examination content and P cylinder filling practice examination video
Makefile demo
Write it down once Net travel management background CPU Explosion Analysis
Use of sigaction
Download and install captura and configure ffmpeg in captura
pytorch是什么?pytorch是一个软件吗?
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
Ffmpeg download and installation tutorial and introduction
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥