【题解】【直方图】【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 ...
随机推荐
- oracle表分区【转】
摘要:在大量业务数据处理的项目中,可以考虑使用分区表来提高应用系统的性能并方便数据管理,本文详细介绍了分区表的使用. 在大型的企业应用或企业级的数据库应用中,要处理的数据量通常可以达到几十 ...
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...
- CentOS Hadoop格式化HDFS异常java.net.UnknownHostException
#bin/hadoop namenode -format DEPRECATED: Use of this script to execute hdfs command is deprecated. I ...
- MyEclipse 修改代码不生效
最近得了一个项目,java开发的web项目,修改代码时,无论怎么改,都不生效: 各种度娘,没用. 原因是没有建立发布设定 这个东西我开始不理解它的作用,现在知道了: mysqleclipse项目在一个 ...
- PowerMock使用遇到的问题——2
如果在测一个类的某一个方法时,这个方法还调用了此类的其他方法,那么如何指定其他方法的返回值呢? Partial mock local private method or public method i ...
- Rsync+sersync文件实时同步
一.为什么要用Rsync+sersync架构1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改 ...
- Python中的sorted函数以及operator.itemgetter函数 【转载】
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- C++封装库
1.新建项目 -> Win32项目 选择DLL , 勾选 空项目 , 点击完成. 2.本例程,使用一个CPP文件 , 及一个头文件. 其中头文件包含函数声明,CPP文件实现函数声明. 3. ...
- java中的日志组件-log4j
1.为什么使用日志组件 Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT的事件记录器:还可以控制每一 ...
- FZU 2082 过路费
树链剖分模板题 #include <cstdio> #include <iostream> #include <cstring> #include <algo ...