-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap.py
More file actions
55 lines (49 loc) · 1.42 KB
/
trap.py
File metadata and controls
55 lines (49 loc) · 1.42 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : trap.py
@Contact : 9824373@qq.com
@License : (C)Copyright 2017-2018, Zhan
@Desc :
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2019/5/7 20:14 zhan 1.0 None
'''
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if len(height) <= 2:
return 0
nonZeroIdx = 0
for i, hi in enumerate(height):
if hi != 0:
nonZeroIdx = i
break
height = height[nonZeroIdx:]
startEnd = height[0]
stopEnd = 0
stopIdx = 1
for i in range(1, len(height), ):
hi = height[i]
if hi >= startEnd:
stopEnd = hi
stopIdx = i
break
if hi >= stopEnd:
stopEnd = hi
stopIdx = i
belowV = min(startEnd, stopEnd)
curVol = 0
for i in range(1, stopIdx):
curVol += belowV - height[i]
if stopIdx == len(height) - 1:
return curVol
else:
return curVol + self.trap(height[stopIdx:])
if __name__ == '__main__':
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
height = []
print(Solution().trap(height))