[leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/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!
解题思路:模拟法。开辟一个数组leftmosthigh,leftmosthigh[i]为A[i]之前的最高的bar值,然后从后面开始遍历,用rightmax来记录从后向前遍历遇到的最大bar值,那么min(leftmosthigh[i], rightmax)-A[i]就是在第i个bar可以储存的水量。例如当i=9时,此时leftmosthigh[9]=3,而rightmax=2,则储水量为2-1=1,依次类推即可。这种方法还是很巧妙的。时间复杂度为O(N)。
代码:
class Solution:
# @param A, a list of integers
# @return an integer
def trap(self, A):
leftmosthigh = [0 for i in range(len(A))]
leftmax = 0
for i in range(len(A)):
if A[i] > leftmax: leftmax = A[i]
leftmosthigh[i] = leftmax
sum = 0
rightmax = 0
for i in reversed(range(len(A))):
if A[i] > rightmax: rightmax = A[i]
if min(rightmax, leftmosthigh[i]) > A[i]:
sum += min(rightmax, leftmosthigh[i]) - A[i]
return sum
[leetcode]Trapping Rain Water @ Python的更多相关文章
- [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
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 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- [ 转载 ] Tcp三次握手和四次挥手详解
#TCP的报头: 源端口号:表示发送端端口号,字段长为16位.目标端口号:表示接收端口号,字段长为16位.序列号:表示发送数据的位置,字段长为32位.每发送一次数据,就累加一次该数据字节数的大小.注意 ...
- 信号量Semaphore的使用
一.概念 Semaphore是一个计数信号量,常用于限制可以访问某些资源(物理或逻辑的)线程数目. 一个信号量有且仅有3种操作,且它们全部是原子的:初始化.增加和减少 增加可以为一个进程解除阻塞: 减 ...
- CodeForces 794 G.Replace All
CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...
- 计蒜客 NOIP 提高组模拟竞赛第一试 补记
计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...
- Java多线程runnable
主要为大家分享Java多线程怎么实现Runnable方式 一 :主要步骤 1.定义实现Runnable接口 2.覆盖Runnable接口中run方法,将线程要运行的代码存在run方法里 3.用Thre ...
- 关于使用react的思考
1. 组件化开发:将可以复用的部分独立封装成一个组件,每个部分的数据互不影响
- 使用 IntraWeb (10) - CSS
IW 会把大多数的视觉属性转换为 CSS; 我们主动使用 CSS 要分两步: 第一步: {通过窗体的 StyleSheet 属性指定要链接的 CSS 文件} procedure TIWForm1.IW ...
- 使用UltraISO制作Windows 10启动U盘
1.从官方网站下载制作工具UltraISO:http://cn.ultraiso.net/uiso9_cn.exe 这是个试用版,但也足够用一次了. 2.在电脑上插入一块U盘,容量最好不少于8GB,接 ...
- Redis-audit工具使用(转)
在我的线上环境中,由于应用上对redis数据没有做冷热处理,所以经常会出现redis内存使用率居高不下的情况,一直以来都想知道都是什么样的数据比较消耗redis内存,就好比写一个sql语句放在数据库中 ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(六)——调试
chrome强大的调试功能令许多开发者爱不释手,在使用cef的时候,我们也可以继承这强大的开发者工具. 集成调试: 我们可以使用如下函数直接使用集成在chrome里的开发者工具 _chrome.Sho ...