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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- nsight 中出现method could not be resolved 报错
解决的方法就是现在编译选项中取消该报错. 项目右键->属性->c/c++常规->Code Analysis,选择"Use project settings" 中 ...
- 使用C#版OpenCV进行圆心求取
OpenCVSharp是OpenCV的.NET wrapper,是一名日本工程师开发的,项目地址为:https://github.com/shimat/opencvsharp. 该源码是 BSD开放协 ...
- python:使用OpenSSL
(一)安装pyOpenSSL 1.下载并安装 https://launchpad.net/pyopenssl pyOpenSSL-0.11.winxp32-py2.7.msi ...
- hihoCoder #1143 : 骨牌覆盖问题·一 (斐波那契数列)
题意:我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘.对于这个棋盘,一共有多少种不同的覆盖方法呢? 思路:这是斐波那契数列啊,f[n] = f[n-1] + f[n-2],初始时 f[ ...
- C语言——字符串长度的计算方法
1.不带转义字符的字符串 如:“abc!x=/”,其长度为7 2.带转义字符的字符串 (1) 字符串“abc\n”:其中的'\n'为转义字符(换行符),计算字符串长度时只能计作一个字符,所以该字符串的 ...
- sk-learning(1)
sk-learning学习笔记(1) 简介 scikit learning 是一个python的机器学习库,内置许多机器学习的算法诸如svm.随机森林.逻辑回归.贝叶斯网络等算法.覆盖了分类.聚类.回 ...
- NopCommerce 3.80框架研究(三)替换tinymce 为KindEditor
NopCommerce 自带的编辑器tinymce 功能不是很全.所以尝试将其替换为功能更强大的 KindEditor 并替实现文件上传和在线浏览功能 首先下载 并解压到如下位置 请注意这里是部署在N ...
- this.value = this.placeholder || this.getAttribute('placeholder')
this.value = this.placeholder || this.getAttribute('placeholder') 鉴于不同的浏览器对为止属性的实现方式有所不用,这里同时使用了HTML ...
- scrum敏捷开发☞
scrum是什么?说白了就是自带一些规则的工具,团队成员按照scrum的规则计划项目,执行项目,完成项目..可以让团队提高工作效率 当前除了scrum还有其他很多类似的像Kanban,XP,RUP(规 ...
- Oracle数据库学习(四)
11.创建表 crate table tab1(f_id number not null,f_a varchar2(7) not null,f_b number(6,2) not null): 主键: ...