Trapping Rain Water 接雨水 python
来源:互联网 发布:局域网电话软件 编辑:程序博客网 时间:2024/05/12 20:14
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.
思路分析:
找到最高柱子,从两端逼近,只要是递增不可能存雨水,需要一个peak移动着时刻记录移动中遇到的临时最高的柱子。时间复杂度O(n)
代码如下:
# coding=utf-8"""接雨水"""class Solution(object): def trap_rainwater(self, listp): movepeak = 0 triprain = 0 maxindex = 0 for i in range(1, len(listp)): if listp[i] > listp[maxindex]: maxindex = i for i in range(0, maxindex): if movepeak < listp[i]: movepeak = listp[i] else: triprain += movepeak - listp[i] movepeak = 0 for j in range(len(listp) - 1, maxindex, -1): if movepeak < listp[j]: movepeak = listp[j] else: triprain += movepeak - listp[j] return triprainif __name__ == '__main__': s = Solution() listp = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] res = s.trap_rainwater(listp) print res
阅读全文
0 0
- Trapping Rain Water 接雨水 python
- **[Lintcode]Trapping Rain Water 接雨水
- 接雨水最大面积Trapping Rain Water
- Trapping Rain Water(捕获雨水)
- LeetCode--Trapping Rain Water(捕获雨水)Python
- 363.Trapping Rain Water-接雨水(中等题)
- Trapping Rain Water 捕获最多的雨水
- [LeetCode] Trapping Rain Water 搜集雨水
- LeetCode:Trapping Rain Water(装雨水问题)
- 42. Trapping Rain Water(捕获雨水)
- LeetCode Trapping Rain Water等雨水的凹槽容量
- LeetCode - Trapping Rain Water 等雨水的凹槽容量
- LeetCode OJ 之 Trapping Rain Water (“收集”雨水)
- Leetcode #42. Trapping Rain Water 雨水收集 解题报告
- LeetCode 42. Trapping Rain Water(装雨水)
- LeetCode------42. Trapping Rain Water(捕获雨水量)
- Leetcode刷题记——Trapping Rain Water(捕获雨水)
- LeetCode-42. Trapping Rain Water (JAVA)收集雨水
- 深度学习各种常见的Net
- linux:使用shell读写MySQL数据库
- 机器学习原来如此有趣!全世界最简单的机器学习入门指南
- HTTP协议
- 数组扩展
- Trapping Rain Water 接雨水 python
- 本地Eclipse访问虚拟机Hadoop需要注意的几点
- Google StyleGuide 链接
- Spark基础-Scala中actor的操作
- java继承与多态
- Mongo 聚合框架-Aggregate(一)
- TCP/IP分层模型与网络认知
- iOS blcok原理
- 我用Python爬了12万条影评,告诉你《战狼Ⅱ》都在说些啥