[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 integerdef trap(self, A):leftmosthigh = [0 for i in range(len(A))]leftmax = 0for i in range(len(A)):if A[i] > leftmax: leftmax = A[i]leftmosthigh[i] = leftmaxsum = 0rightmax = 0for 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
题目描述:(链接) Given n non-negative integers representing an elevation map where the width of each bar is ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain Water Given ...
- 【重点:BFS】LeetCode 407. Trapping Rain Water II
LeetCode 407. Trapping Rain Water II 博客转载自:http://www.cnblogs.com/grandyang/p/5928987.html [太难了,被智商碾 ...
- 【重点:DP 双指针 栈】LeetCode 42. Trapping Rain Water
LeetCode 42. Trapping Rain Water 本博客转载自:http://www.cnblogs.com/grandyang/p/4402392.html [自己又不会做,抄的-& ...
- LeetCode 42 Trapping Rain Water 收集雨水
LeetCode 42 Trapping Rain Water 收集雨水 Given n non-negative integers representing an elevation map whe ...
- Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告
1 解题思想 我看了下题目,发现比预想中的简单,加之我比较烂,所以其实我还是没做,只是看懂了上回贴的代码,然后做了一下注释,现在我来讲下题目. 首先请看下上一题,上一题是2D的这题是3D的: Leet ...
- Leetcode 动态规划 Trapping Rain Water
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Tota ...
- LeetCode42. Trapping Rain Water
LeetCode42. Trapping Rain Water 原题地址 题目描述 Given n non-negative integers representing an elevation ma ...
最新文章
- 如何使python print输出不换行
- 中国-中东欧国家特色农产品 云上国际农民丰收节贸易会
- [项目管理]团队管理中的起点:尊重
- Android Monkey测试入门(摘)
- 1095. 山脉数组中查找目标值
- 芯片支持的且会被用到的H.264特性 预测编码基本原理
- Java SSM6——SSM整合
- 机器人卡纸形状图片大全图片_天水生态木天花吊顶图片大全
- 菜式介绍:风味黄泥烤鸡
- DataReader与DataAdapter的区别
- 【连载】第五章机械振动第一节——简谐振动
- API函数的调用过程
- java mysql sqlhelper_SQLHelper 的 java版本 - 转载
- 嵌入式面试经验分享1——应届生
- icon小图标的使用
- 如何零基础制作一款自己的游戏!(一)
- LaTeX行距以及字体大小
- 华为ESNP,路由器和交换机连接电脑不同网段的解决方法。
- 暑期参加CSDN编程竞赛的些许心得体会
- TPM-TPM-Profile-PTP协议-2
热门文章
- javascript调试工具
- SpringBoot原理
- C++入门经典-例8.3-子类显示调用父类构造函数
- [BZOJ 4034][HAOI2015]树上操作(欧拉序列+线段树)
- 关于变量作用域的一点整理
- 有关android 应用的plugin框架调研
- JQuery官方学习资料(译):$( document ).ready()
- 手持GPS坐标系统的转换与应用
- SQL Server 的分散式資料複寫技術
- PetShop之ASP.NET缓存