接雨水
来源:互联网 发布:知乎jennyyy扒皮照片 编辑:程序博客网 时间:2024/06/03 02:06
网址:http://www.lintcode.com/zh-cn/problem/trapping-rain-water/
题目描述:
给出 n 个非负整数,代表一张X轴上每个区域宽度为 1
的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。
样例
如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1]
, 返回 6
.
思路:计算出每个坐标的左右海拔的最高值,然后取其最小值和自身海拔相比,假若比自己高,则在结果上加上其能接到的雨水(即两边海拔的最小值减去自身海拔)
代码:
import java.lang.Math;public class Solution { /** * @param heights: an array of integers * @return: a integer */ public int trapRainWater(int[] heights) { // write your code here int R=0,len = heights.length; int left[] = new int[heights.length]; int right[] = new int[heights.length]; for(int i=0;i<len;++i){ if(i==0) left[0]=0; else left[i] = Math.max(left[i-1],heights[i-1]); } for(int i=len-1;i>=0;--i){ if(i==len-1) right[len-1]=0; else right[i] = Math.max(right[i+1],heights[i+1]); int t = Math.min(left[i],right[i]); if(t > heights[i]) R += t-heights[i]; } return R; }}
0 0
- 接雨水
- 接雨水
- 接雨水
- LintCode-接雨水
- LintCode:接雨水
- LintCode:接雨水
- 接雨水 lintcode
- 接雨水问题(C++)
- 接雨水问题
- lintcode[363]:接雨水
- Java实现-接雨水
- LintCode:M-接雨水
- 接雨水-LintCode
- 接雨水 II-LintCode
- LintCode rain trap 接雨水
- [LintCode 363] 接雨水(Python)
- **[Lintcode]Trapping Rain Water 接雨水
- 接雨水最大面积Trapping Rain Water
- Mahout安装配置————转自爱嘉牛LA
- java数组的声明常用的方式
- Git命令使用
- OpenLayers开篇: Hello OpenLayers
- beaglebone black i2s dac使用方法
- 接雨水
- hadoop-2.6.2 配
- eclipse如何导入v7包
- MATLAB中数值拟合的种种办法
- NYOJ588(母函数基础)
- js第六节-作用域
- win7系统mysql安装配置以及卸载
- 京东笔试题
- 如何优雅的处理异常(java)