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.
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!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 My code:
class Solution:
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
overall_count=0
start=0
while start<len(height):
if height[start]<=0:
start+=1
continue
tmp_count=0
tmp_start=start+1
if start+1>=len(height):
start+=1
continue
tmp_height=height[start]
while tmp_start<len(height) and height[tmp_start]<tmp_height:
tmp_count+=tmp_height-height[tmp_start]
tmp_start+=1
print('start %d,tmp_start %d,tmp_count %d'%(start,tmp_start,tmp_count))
if tmp_start<len(height):
overall_count+=tmp_count
if tmp_start>=len(height):
height[start]-=1
else:
start=tmp_start
return overall_count
Solution: https://leetcode.com/problems/trapping-rain-water/