Advertisement
arvind_iyer_2001

Trap Water

Jun 27th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. def trap(self, height: List[int]) -> int:
  2.         if(len(height) == 0):
  3.             return 0
  4.         maxi = max(height)
  5.         maxl = height.index(maxi)
  6.         maxr = len(height) - height[::-1].index(maxi) - 1
  7.        
  8.         h = [0]*len(height)
  9.         h[0] = height[0]
  10.         h[-1] = height[-1]
  11.         for i in range(1,maxr+1):
  12.             if(h[i-1] <= height[i]):
  13.                 h[i] = height[i]
  14.             else:
  15.                 h[i] = h[i-1]
  16.         for i in range(maxr,len(height)-1).__reversed__():
  17.             if(h[i+1] <= height[i]):
  18.                 h[i] = height[i]
  19.             else:
  20.                 h[i] = h[i+1]
  21.         return sum(h) - sum(height)  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement