LintCode:M-接雨水
来源:互联网 发布:艾弗森生涯与麦迪数据 编辑:程序博客网 时间:2024/04/28 18:30
LintCode链接
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.
样例
如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1]
, 返回 6
.
(1)找到最高高度
(2)左右两边分别向最高处逼近,计算第一个比当前高度高的位置距离,以及中间覆盖的低于当前高度的高度总和,用当前高度能盛的最大和减去内部高度和,等于雨水和
public class Solution { /* * @param heights: a list of integers * @return: a integer */ public int trapRainWater(int[] heights) { int n = heights.length; int max=0; int maxIndex=0; for(int i=0; i<n; i++){ if(max<heights[i]){ max = Math.max(max, heights[i]); maxIndex=i; } } int i=0, j=1; int vol=0; while(j<=maxIndex){ int inner=0; while(heights[i]>heights[j]){ inner+=heights[j++]; } vol+=heights[i]*(j-i-1)-inner; i=j++; } i=n-1; j=n-2; while(j>=maxIndex){ int inner=0; while(heights[i]>heights[j]){ inner+=heights[j--]; } vol+=heights[i]*(i-j-1)-inner; i=j--; } return vol; }}
阅读全文
0 0
- LintCode:M-接雨水
- LintCode-接雨水
- LintCode:接雨水
- LintCode:接雨水
- 接雨水 lintcode
- lintcode[363]:接雨水
- 接雨水-LintCode
- 接雨水 II-LintCode
- LintCode rain trap 接雨水
- [LintCode 363] 接雨水(Python)
- 雨水,lintcode
- **[Lintcode]Trapping Rain Water 接雨水
- lintcode刷题——接雨水
- 接雨水
- 接雨水
- 接雨水
- LintCode(M)
- 接雨水问题(C++)
- redis架构设计
- 【常用工具】经验教训
- 如何调研使用一款数据访问层中间件
- Cmake -static编译
- 01:判断数正负
- LintCode:M-接雨水
- 求助!!!!Failed to load ApplicationContext错误
- 2017多校联合第六场String/hdu 6096 (tire tree/ac自动机)
- av_read_frame解析
- <NotePad++>字体格式设置
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- 解决Python Pandas 读取Oracle数据库中文乱码问题
- 安全框架Apache Shiro学习心得
- hbase-spark全新的spark读写hbase的方式