leetcode 【 Trapping Rain Water 】python 实现
题目:
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!
代码:oj测试通过 Runtime: 91 ms
class Solution:
# @param A, a list of integers
# @return an integer
def trap(self, A):
# special case
if len(A)<3:
return 0
# left most & right most
LENGTH=len(A)
left_most = [0 for i in range(LENGTH)]
right_most = [0 for i in range(LENGTH)]
curr_max = 0
for i in range(LENGTH):
if A[i] > curr_max:
curr_max = A[i]
left_most[i] = curr_max
curr_max = 0
for i in range(LENGTH-1,-1,-1):
if A[i] > curr_max:
curr_max = A[i]
right_most[i] = curr_max
# sum the trap
sum = 0
for i in range(LENGTH):
sum = sum + max(0,min(left_most[i],right_most[i])-A[i])
return sum
思路:
一句话:某个Position能放多少水,取决于左右两边最小的有这个Position的位置高。
可以想象一下物理环境,一个位置要能存住水,就得保证这个Position处于一个低洼的位置。怎么才能满足低洼位置的条件呢?左右两边都得有比这个position高的元素。如何才能保证左右两边都有比这个position高的元素存在呢?只要左右两边的最大值中较小的一个比这个Position大就可以了。
leetcode 【 Trapping Rain Water 】python 实现的更多相关文章
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- [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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- C#利用WebClient 两种方式下载文件(一)
WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...
- C#基础知识图谱
- Flexbox与Grid属性比较
网格容器(container)属性 网格项目(item)属性 Flex容器(container)属性 Flex项目(item)属性
- SQLSERVER 创建ODBC 报错的解决办法 SQLState:'01000'的解决方案
错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOp ...
- CAsyncSocket create创建套接字失败
解决方法: 在继承CAsyncSocket 类的子类的构造函数中加入以下代码: if (!AfxSocketInit()) AfxMessageBox(IDP_SOCKETS_INIT_FAILED) ...
- IOS NSKeyedArchiver(归档存取数据)
如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类 型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法 ...
- iOS 真机报错 Command CodeSign failed with a nonzero exit code
今天在网上下载的一个小demo,在模拟器上正常运行,但是在真机上报错: 解决方式: 1.打开钥匙串:2.锁住login keychain: 3.再把它解锁. 然后就能真机上正常运行了,
- sessionStorage 和 localStorage
html5 中的 web Storage 包括了两种存储方式:sessionStorage 和 localStorage. sessionStorage 用于本地存储一个会话(session)中的数据 ...
- Bootstrap 弹出框(Popover)插件
Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...
- react 信用卡格式检验
前言: 技术栈主要基于react + ant-design 描述: 填写信用卡卡号时,会自动四位空格,并格式校验判断卡种 ,这里我们业务只涉及到四种卡. 代码解析 // ant 组件自引,这里我只讲 ...