-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap_rain_water.py
42 lines (36 loc) · 1.29 KB
/
trap_rain_water.py
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
from typing import List
class Solution:
def trap(self, height: List[int]) -> int:
"""
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
"""
left = 0
right = len(height) - 1
left_max = 0
right_max = 0
result = 0
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
result += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
result += right_max - height[right]
right -= 1
return result
# Time complexity: O(N) where N is the length of the height list.
# Space complexity: O(1).
import unittest
class TestTrapRainWater(unittest.TestCase):
def setUp(self) -> None:
self.solution = Solution()
def test_trap_rain_water(self) -> None:
self.assertEqual(self.solution.trap([0,1,0,2,1,0,1,3,2,1,2,1]), 6)
self.assertEqual(self.solution.trap([4,2,0,3,2,5]), 9)
if __name__ == '__main__':
unittest.main()