-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.py
More file actions
31 lines (25 loc) · 651 Bytes
/
42.py
File metadata and controls
31 lines (25 loc) · 651 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
class Solution:
def trap(self, height):
left = 0
right = len(height) - 1
pre = suf = 0
ans = 0
while left < right:
pre = max(pre,height[left])
suf = max(suf,height[right])
if pre < suf:
ans += pre - height[left]
left += 1
else:
ans += suf - height[right]
right -=1
return ans
def main():
line = sys.stdin.read().strip().split(',')
nums = list(map(int,line))
sol = Solution()
result = sol.trap(nums)
print(result)
if __name__ == "__main__":
main()