您的位置:首页 > 移动开发

算法分析课每周练习 Trapping Rain Water

2017-07-05 22:37 381 查看
题目

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.

分析

class Solution:
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
leftmosthigh = [0 for i in range(len(height))]
leftmax = 0
for i in range(len(height)):
if height[i] > leftmax: leftmax = height[i]
leftmosthigh[i] = leftmax
sum = 0
rightmax = 0
for i in reversed(range(len(height))):
if height[i] > rightmax: rightmax = height[i]
if min(rightmax, leftmosthigh[i]) > height[i]:
sum += min(rightmax, leftmosthigh[i]) - height[i]
return sum这就是我室友面试时遇到的题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: