-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ42.py
More file actions
27 lines (25 loc) · 773 Bytes
/
Q42.py
File metadata and controls
27 lines (25 loc) · 773 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
class Solution:
def trap(self, height) -> int:
if not height or len(height) == 1 or len(height) == 2:
return 0
count = 0
flag = height[0]
fl_index = 0
res = 0
for index, item in enumerate(height):
if index == 0:
continue
if item >= flag:
temp = (index - fl_index - 1) * min(item, flag)
res += temp - count
fl_index = index
flag = item
else:
count += item
if fl_index < len(height) - 2:
res_temp = self.trap(height[fl_index + 1:])
res = res + res_temp
return res
if __name__=='__main__':
so = Solution()
print(so.trap([4,2,3]))