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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- 构建第一个Spring Boot2.0应用之Controller(三)
Controller控制器主要是接收浏览器请求.下面说一说@Controller注解和@RestController的区别: (1)@Controller类中的方法可以直接通过返回String跳转到j ...
- Linux命令行环境与桌面环境护切换
1.前言 在大部分情况下,我们在使用Linux时习惯使用命令行环境,但是有时候也还是会使用到安装桌面环境,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境.以Centos 6.5 为例演示一 ...
- Windows系统HTTP身份验证方法
当Windows客户端尝试使用HTTP协议访问基于Web的资源时,会在客户端和服务器之间建立"对话".换句话说,服务器告诉客户端,访问资源之前进行身份验证 ,并且服务器还告诉客户端 ...
- jQuery_2_常规选择器-高级选择器2
属性选择器 <a title="num1">num1</a> <a title="num-ad">num2</a> ...
- [Asp.Net] Form验证中 user.identity为false
这个方法可以是user.identity设置为true FormsAuthentication.SetAuthCookie(Username, true); 但是要开启form验证, 在配置文件中 & ...
- Ruby 学习笔记(一)
环境搭建 本文基于Mac OS,windowns坑较多,建议使用Mac. xcode-select -p 检查是否安装xcode-select, 如果没有,通过xcode-select --insta ...
- hdu-1598 find the most comfortable road---kruskal+枚举下界
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Ro ...
- 分析ELF的加载过程
http://blog.chinaunix.net/uid-72446-id-2060538.html 对于可执行文件来说,段的加载位置是固定的,程序段表中如实反映了段的加载地址.对于共享库来?段的加 ...
- python读取excel中的数据
import numpy as np import matplotlib.pyplot as plt import pandas as pd #df = pd.read_excel('/Users/N ...
- 分享12款最佳的Bootstrap设计工具
设计师总会渴望有一些新奇有趣的设计工具来提高工作效率,而Bootstrap就是您的不二选择.2013年Bootstrap得到了广泛普及, 它是开发者较为常用的框架之一,本文我们将分享12款最佳的Boo ...