From 3618020006614400791496b3238aef81bf04f882 Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Mon, 30 Oct 2023 15:39:49 +0900 Subject: [PATCH 1/2] Create readme --- Hard/42. Trapping Rain Water/Approach 1/sol.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Hard/42. Trapping Rain Water/Approach 1/sol.md diff --git a/Hard/42. Trapping Rain Water/Approach 1/sol.md b/Hard/42. Trapping Rain Water/Approach 1/sol.md new file mode 100644 index 0000000..9cdf663 --- /dev/null +++ b/Hard/42. Trapping Rain Water/Approach 1/sol.md @@ -0,0 +1,13 @@ +1. The code first checks if the input elevation map, height, is empty. If it is, it returns 0 immediately, signifying no trapped water. +2. Two arrays, lm (left maximum) and rm (right maximum), are initialized to store the maximum heights to the left and right of each position, respectively. +3. Starting from the leftmost position of the elevation map, the code calculates and stores the maximum height to the left (including the current position) in the lm array. +4. Similarly, beginning from the rightmost position and moving leftwards, the maximum height to the right (including the current position) is calculated and stored in the rm array. +5. Leveraging the two pre-computed arrays, the trapped water at each position is calculated. This is done by determining the minimum of the left and right maximum heights for each position and subtracting the height of the current position. +6. The trapped water values for all positions are summed to yield the total trapped water. +## Time Complexity: +The **time complexity** of this approach is + +**O(n)**, where n is the number of elements in the elevation map. This is because each element is processed thrice: once for the lm array, once for the rm array, and once for the final trapped water calculation. + +**Space Complexity**: +The space complexity of the algorithm is **O(n)** due to the two additional arrays (lm and rm) used to store the maximum heights to the left and right of each position. These arrays linearly scale with the number of elements in the input elevation map. \ No newline at end of file From da0de20f7eb0c8c84c2c739b16a1b777bd70bafc Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Mon, 30 Oct 2023 15:40:04 +0900 Subject: [PATCH 2/2] Solution for problem 42 with Python --- .../Approach 1/solution.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Hard/42. Trapping Rain Water/Approach 1/solution.py 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