Skip to content

Commit

Permalink
Create readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-per-ardua committed Oct 30, 2023
1 parent 97f9eb1 commit 3618020
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Hard/42. Trapping Rain Water/Approach 1/sol.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 3618020

Please sign in to comment.