【题解】【直方图】【Leetcode】Trapping Rain Water
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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
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!
思路:
O(n) solution. for each bar, find the max height bar on the left and right. then for this bar it can hold min(max_left, max_right) - height
对于任何一个坐标,检查其左右的最大坐标,然后相减就是容积。所以,
1. 从左往右扫描一遍,对于每一个坐标,求取左边最大值。
2. 从右往左扫描一遍,对于每一个坐标,求最大右值。
直方图的题还有Container With Most Water和Largest Rectangle in Histogram,还可以看看Maximal Rectangle,都很有意思
代码:
int trap(int A[], int n) {
if(n < )
return ; vector<int> maxRs(n);
int maxR = ;
for(int i = ; i < n; i++){
if(A[i] > maxR)
maxR = A[i];
maxRs[i] = maxR;
} int totalV = ;
int maxL = ;
for(int i = n-; i >= ; i--){
if(A[i] > maxL)
maxL = A[i];
totalV += min(maxL, maxRs[i]) - A[i];
}
return totalV;
}
【题解】【直方图】【Leetcode】Trapping Rain Water的更多相关文章
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- Leetcode: Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- kafka技术要点
转载:http://blog.csdn.net/caisini_vc/article/details/48007297 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为 ...
- 盘点十大最流行的Linux服务器发行版
随着Linux不断发展,Linux所支持的文件系统类型也在迅速扩充.很多的数据中心服务器上都运行着Linux,可以节省大量的许可证费用及维护费用.但伴随着Linux新版本的发行,其中每一个不同版本的L ...
- Linux创建LVM
###########format disk############ 格式化磁盘,将其SystemId修改为8e fdisk /dev/sdb n p 1 [enter] [enter] t 8e w ...
- spring aop编程
1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代 ...
- Mac android 开发 sdk配置和手机连接
本文适合已经很熟悉android开发的人员: 首先安装Mac版的eclipse 其次是android sdk的准备: 由于android sdk在线更新很不方便,所以可以选择复制:准备好Mac下的an ...
- Understanding Virtual Memory
Understanding Virtual Memory by Norm Murray and Neil Horman Introduction Definitions The Life of a P ...
- UVa 11427 - Expect the Expected
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ECMAScript 6新特性(1)数组篇
数组现有的方法: .concat():连接两个或更多的数组,并返回结果. .join():把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. .pop():删除并返回数组的最后一个元素 . ...
- [开发笔记]-Windows Service服务相关注意事项
注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...
- const的全面理解
const关键字用来作甚?const是一个类型修饰符.常见的类型修饰符有哪些? short long unsigned signed static autoextern register 定义一个变量 ...