diff --git a/Hard/42. Trapping Rain Water/Approach 1/solution.py b/Hard/42. Trapping Rain Water/Approach 1/solution.py new file mode 100644 index 0000000..480193f --- /dev/null +++ b/Hard/42. Trapping Rain Water/Approach 1/solution.py @@ -0,0 +1,21 @@ +class Solution: + def trap(self, height: List[int]) -> int: + if not height: + return 0 + n = len(height) + lm = [0] * n + rm = [0] * n + + lm[0] = height[0] + for i in range(1, n): + lm[i] = max(lm[i - 1], height[i]) + + rm[n - 1] = height[n - 1] + for i in range(n - 2, -1, -1): + rm[i] = max(rm[i + 1], height[i]) + + water = 0 + for i in range(n): + water += min(lm[i], rm[i]) - height[i] + + return water \ No newline at end of file