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 ...
随机推荐
- djangle中模板系统的使用
django相关的命令行命令: 创建一个djaongo的应用:在已经创建号的应用文件夹中运行:django-admin.py startproject projectName 开启系统自带的服务器在网 ...
- 在JS中统计函数执行次数
一.统计函数执行次数 常规的方法可以使用 console.log 输出来肉眼计算有多少个输出 不过在Chrome中内置了一个 console.count 方法,可以统计一个字符串输出的次数.我们可以利 ...
- python之正则匹配match:search findall
match:从开头位置匹配,只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+ match(a,"abcdef") 匹配a >>> re.match(&quo ...
- C语言第四次实验报告
第四次实验报告 一·实验项目名称: 多球反弹 二·实验项目功能描述: (1)实现多个小球 (2)实现多个小球碰壁会反弹 (3)实现小球之间碰撞反弹 三· 项目模块结构介绍 #define High 4 ...
- centos7没有IP地址
查看网卡 ip addr查看网卡 我截图中有ip,是因为我已经设置过了. eth0是对外的网卡,我们接下来设置这个网卡,你的网卡名字可能和我的不一样. 修改网卡 修改/etc/sysconfig/ne ...
- sourcetree pull push需要密码问题
我的是mac,以mac版本的sourcetree 为例 第一步 项目仓库右上角设置 第二步.点击远程仓库. 点击仓库路径点击编辑 第三步 url/路径修改 原本.https://gitee.com ...
- 【转】UNITY中相机空间,投影空间的正向问题
原文链接1:https://www.cnblogs.com/wantnon/p/4570188.html 原文链接2:https://www.cnblogs.com/hefee/p/3820610.h ...
- 事务的ACID属性
事务,一个操作序列,这些操作要么都执行,要么都不执行,是一个不可分割的整体. ACID为事务的四大属性 原子性(Atomic):指整个数据库事务是不可分割的工作单位.只有使据库中所有的操作执行成功,才 ...
- C# 图片文件文本string格式 传输问题
string file = @"E:\test.png"; byte[] bytes = File.ReadAllBytes(file); // 主要代码 string datas ...
- Spring的应用上下文ApplicationContext
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ...