42. Trapping Rain Water (JAVA)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
首先找到最高的bar,然后从左到这个最高的bar,依次计算面积;再从右到这个最高的bar,依次计算面积。时间复杂度n*2 = O(n)
class Solution {
public int trap(int[] height) {
int ret = 0;
int curIndex = 0; //index of highest
int topIndex = 0;
for(int i = 1; i < height.length; i++){
if(height[i] > height[topIndex]) topIndex = i;
}
for(int i = 0; i < topIndex; i++){
if(height[i] > height[curIndex]){
curIndex = i;
}
else{
ret += (height[curIndex]-height[i]);
}
}
curIndex = height.length-1;
for(int i = height.length-2; i > topIndex; i--){
if(height[i] > height[curIndex]){
curIndex = i;
}
else{
ret += (height[curIndex]-height[i]);
}
}
return ret;
}
}
42. Trapping Rain Water (JAVA)的更多相关文章
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- promql 常用函数介绍
Metrics类型 根据不同监控指标之间的差异,Prometheus定义了4中不同的指标类型(metric type):Counter(计数器).Gauge(仪表盘).Histogram(直方图).S ...
- springboot(三).springboot用最简单的方式整合mybatis
Springboot整合mybatis 在众多的orm框架中,我使用最多的,最习惯的,也是目前使用最广泛的就是mybatis,接下来我们就去将springboot整合mybatis 对于spring ...
- vue几种简单的传值方式
除了一下的几种方式外,可以参考 https://www.cnblogs.com/hpx2020/p/10936279.html 组件传值的方法: 一.父组件向子组件传递数据(props) 第1:父组件 ...
- Mysql中经常出现的乱码问题
Mysql中执行SET NAMES utf8这条SQl的作用 1)首先,Mysql服务器的编码和数据库的编码在配置文件my.ini中设置: 用记事本打开配置文件,修改代码:default-charac ...
- lr参数与C语言函数参数的区别
C变量不能再lr函数中使用: c变量必须定义在lr函数之前: LR参数可以在LR函数中直接当做字符串使用. LR参数是lr自己封装的一个钟对象, LR参数的表达方式:{ParamName}
- final finalize finally throw throws try catch
什么是finalize()方法 finalize()方法什么时候被调用 参见网址 析构函数(finalization)的目的是什么 final 和 finalize 的区别 final以下参见网址 f ...
- Windows程序调用dll
可以写在WndProc的WM_CREATE里面,不能写在WinMain里面
- java常用加密算法
常用加密算法的Java实现(一) ——单向加密算法MD5和SHA 日期:2014/6/1 文:阿蜜果 1.Java的安全体系架构 1.1 Java的安全体系架构介绍 Java中为安 ...
- CentOS 6、CentOS7 防火墙开放指定端口
当我们在CentOS服务器中装了一些开发环境(如 tomcat.mysql.nginx 等...)时,希望能从外界访问,就需要配置防火墙对指定端口开放. CentOS 6.51.开放指定端口/sbin ...
- Python Module_oslo.vmware_连接 vCenter
目录 目录 前言 Install oslsvmware How to use the vSphere Web Service SDK 前言 oslo.vmware 是一个由 Python 实现的 vC ...