leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 for i in range(len(nums)): if nums[i] > leftmax: leftmax=nums[i] leftmosthigh[i] = leftmax print leftmosthigh sums=0 rightmax=0 for i in reversed(range(len(nums))): print i if nums[i] > rightmax: rightmax = nums[i] print 'i is',i,'rightmax is',rightmax #current i less than it's two side, then it can trap if min(rightmax,leftmosthigh[i] ) > nums[i]: sums+=min(rightmax,leftmosthigh[i])-nums[i] return sums nums= [0,1,0,2,1,0,1,3,2,1,2,1] obj=Solution() rs=obj.trap(nums) print rs