当前位置:网站首页>【刷题篇】接雨水(一维)
【刷题篇】接雨水(一维)
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;
}
边栏推荐
- Avec trois. JS fait une scène 3D simple
- Appium自动化测试框架
- npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
- pytorch怎么下载?pytorch在哪里下载?
- Use of sigaction
- 用Three.js做一個簡單的3D場景
- C语言HashTable/HashSet库汇总
- sigaction的使用
- Téléchargement et installation du client Filezilla
猜你喜欢

Mysql Mac版下载安装教程

Recursion: one dimensional linked lists and arrays

How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo

简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息

Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check

pytorch是什么?pytorch是一个软件吗?

递归:快速排序,归并排序和堆排序

编译文件时报错:错误: 编码GBK的不可映射字符
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]

Is pytorch difficult to learn? How to learn pytorch well?
随机推荐
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
Use of sigaction
Read a paper_ ChineseBert
Using jasmine to monitor constructors - spying on a constructor using Jasmine
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
C语言HashTable/HashSet库汇总
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
【学习笔记】seckill-秒杀项目--(11)项目总结
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
小程序获取用户头像和昵称
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
leetcode:297. 二叉树的序列化与反序列化
Ffmpeg one / more pictures synthetic video
Use three JS make a simple 3D scene
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Filter
docker安装及启动mysql服务
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
2022 P cylinder filling examination content and P cylinder filling practice examination video
用Three.js做一個簡單的3D場景