lintcode:接雨水
给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。

如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返回 6.
解题
先遍历一遍找到最高点,然后分别从两边开始,往最高点所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。
左到最高点,海拔高度下降的时候计算水位,左边最近比他高的海拔 减去 自己的海拔 ,这是该海拔的水位量。当遇到更高的海拔的时候更新海拔高度。
右到最高点,海拔高度下降的时候计算水位, 右边最近比他高的海拔 减去 自己的海拔 ,这是该海拔的水位量。当遇到更高的海拔的时候更新海拔高度。
public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] A) {
// write your code here
int n = A.length;
if(n <= 2) return 0;
int max = -1, maxInd = 0;
int i = 0;
for(; i < n; ++i){
if(A[i] > max){
max = A[i];
maxInd = i;
}
}
int area = 0, root = A[0];
for(i = 0; i < maxInd; ++i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
for(i = n-1, root = A[n-1]; i > maxInd; --i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
return area;
}
}
Java Code
class Solution:
# @param heights: a list of integers
# @return: a integer
def trapRainWater(self, A):
# write your code here
n = len(A)
if(n <= 2):
return 0
max = -1
maxInd = 0
for i in range(n):
if A[i]> max:
max = A[i]
maxInd = i
leftMax = A[0]
area = 0
for i in range(maxInd):
if leftMax < A[i]:
leftMax = A[i]
else:
area = area + leftMax - A[i] rightMax = A[n-1]
for i in range(n-1,maxInd,-1):
if rightMax< A[i]:
rightMax = A[i]
else:
area = area + rightMax - A[i] return area
Python Code
lintcode:接雨水的更多相关文章
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
随机推荐
- c/c++常用代码 -- 共享内存
#pragma once #include <stdio.h> #include <tchar.h> #include <string.h> #include &l ...
- POC - ASP.NET & IIS 部分
终于得到了我VM的管理员权限啦啦.接下来不需要把IIS架在我自己的电脑上了,将架在我的VM上. 1. 先添加ISAP和CGI的组件. 2. 将defaultAppPool的MODE设为CLASSIC, ...
- Mono for Android (2)-- Android应用程序初认识
一:日志记录 先添加using Android.Util; 在该命名控件下有log类 Log.Info("HA", "End onCreate"); //记录消 ...
- How to compile and install NCAR Command Language on IBM PowerPC 64 --- NCL编译安装步骤
作者:Sinsonglew 出处:http://www.cnblogs.com/sinsonglew 欢迎转载,也请保留这段声明.thanks :) 注记:NCL官方依赖安装包全集列表.官方源码编译指 ...
- BICEP单元测试计划——四则运算Ⅱ
一.测试方法(Right-BICEP) 6个值得测试的具体部位: Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你 ...
- 程序开发心理学阅读笔记——第I篇
1.软件的任务是为了解决某一特定的问题,而软件开发者的任务却需要解决一系列问题.2.温伯格说,我们不能要求每个人都聪明异常,能够解决所有难题:但是我们必须持续思考,因为只有如此,我们才能明白自己在做什 ...
- jQuery+css3弹出框插件
先来看DEMO:https://codepen.io/jonechen/pen/regjGG 插件的开发很简单,运用了CSS3的动画效果,并且弹出框的内容可以自定义.插件的默认配置参数有三个: var ...
- Material
renderer.material 物理材质 实现二维图上的人物动作 新建Material,选择Shader(著色器)为transparent/diffuse(背景透明),将上图拉到背景图选项中. ...
- quartz2D简单使用
quartz2D绘图 1:上下文:context,这个翻译不好理解,其实翻译环境更好一点,就是给了你一个画板,你看不到而已 在: CGContextRef ctx = UIGraphicsGetCur ...
- js判断浏览器滚动条是否拉到底
$(window).scroll(function(){ // 当滚动到最底部以上n像素时, 加载新内容 if ($(document).height() - $(this).scrollTop() ...