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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- FusionCharts使用教程:为JavaScript图表提供数据
FusionCharts的JavaScript类提供了一系列的函数来提供图表数据. FusionCharts的JavaScript类支持XML或JSON格式的数据.这些数据可以是URL或字符串. 以X ...
- Git命令--保存用户名和密码
使用git各项操作时,总是会出现输入密码的弹窗,且需要多次输入,很是繁琐,通过git命令可以记住密码,避免多次操作. 一.创建保存密码的文件 1.在home文件夹,一般是 C:\Documents a ...
- 调用cmd命令行命令(借鉴)
留待以后观看 ———————————————————————————————————————————————————————————————————————————— public class IP_ ...
- @Valid的坑
@Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前
- 关于docker容器内核参数修改问题
以下内容截取自docker官方文档 地址:https://docs.docker.com/edge/engine/reference/commandline/run/#configure-namesp ...
- linux常用工具
命令 功能应用 用法举例 free 查看内存使用情况,包括物理内存和虚拟内存 free -h或free -m vmstat 对系统的整体情况进行统计,包括内核进程.虚拟内存.磁盘.陷阱和 CPU 活动 ...
- html body上有一条空白!!!
html body 上莫名其妙的就出现了一条空白,怎么搞都搞不定,弄了一下午...... 解决了!!! 格式问题/
- windows phone 8.0 的网络图片异步加载方案
买了一本林政的8.1UI的书,看到一个使用弱引用对像来解决图片缓存的问题,刚好自已写的应用也遇到这个问题,于是小改动了一下代码,搬到了8.0版本来使用,代码由 zhxilin℃+ 大神提供了部分解决代 ...
- 解决 Unable to convert MySQL date/time value to System.DateTime
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- 【HDU4507】恨7不成妻(数位DP)
点此看题面 大致题意: 让你求出一段区间内与\(7\)无关的数的平方和.与\(7\)无关的数指整数中任意一位不为\(7\).整数的每一位加起来的和不是\(7\)的整数倍.这个整数不是\(7\)的倍数. ...