File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,20 +6,18 @@ def trap(self, height: List[int]) -> int:
66 if not height :
77 return 0
88
9- h_len = len (height )
10- max_left = [None ] * h_len
11- max_right = [None ] * h_len
12-
13- max_left [0 ] = 0
14- for i in range (1 , h_len ):
15- max_left [i ] = max (max_left [i - 1 ], height [i - 1 ])
9+ left , right = 0 , len (height ) - 1
10+ left_max , right_max = height [left ], height [right ]
11+ res = 0
1612
17- max_right [- 1 ] = 0
18- for i in range (h_len - 2 , - 1 , - 1 ):
19- max_right [i ] = max (max_right [i + 1 ], height [i + 1 ])
13+ while left < right :
14+ if left_max < right_max :
15+ left += 1
16+ left_max = max (left_max , height [left ])
17+ res += left_max - height [left ]
18+ else :
19+ right -= 1
20+ right_max = max (right_max , height [right ])
21+ res += right_max - height [right ]
2022
21- res = 0
22- for i in range (h_len ):
23- trapped = min (max_left [i ], max_right [i ]) - height [i ]
24- res += max (0 , trapped )
2523 return res
You can’t perform that action at this time.
0 commit comments