-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) | ||
|
||
## Intuition | ||
|
||
The intuition behind the problem is to find out how much water can be trapped between the bars of the elevation map | ||
after raining. The amount of water trapped above a bar is determined by the height of the tallest bars to its left and | ||
right. If we know these values, we can calculate the trapped water above each bar by subtracting the bar’s height | ||
from the minimum of the tallest bars to its left and right. | ||
|
||
## Approach | ||
|
||
1. **Precompute Maximum Heights:** | ||
- Use two arrays, `maxLeft` and `maxRight`, to store the maximum heights up to each index from the left and the | ||
right, respectively. | ||
- Iterate through the `height` array to fill these arrays. | ||
- `maxLeft[i]` stores the maximum height of the bars from the left up to index `i`. | ||
- `maxRight[j]` stores the maximum height of the bars from the right up to index `j`. | ||
2. **Calculate Trapped Water:** | ||
- For each bar at index `i`, the water trapped above it is determined by the minimum of `maxLeft[i]` and | ||
`maxRight[i]`, minus the height of the bar itself (`height[i]`). | ||
- Sum these values to get the total amount of trapped water. | ||
3. **Implementation:** | ||
- Initialize `maxLeft` and `maxRight` arrays. | ||
- Iterate through the `height` array from both ends simultaneously to fill `maxLeft` and `maxRight`. | ||
- Iterate through the `height` array to calculate the total trapped water using the precomputed values in `maxLeft` | ||
and `maxRight`. | ||
|
||
## Complexity | ||
|
||
- **Time Complexity: O(N)** | ||
- The first loop to fill `maxLeft` and `maxRight` runs in `O(N)` time. | ||
- The second loop to calculate the trapped water also runs in `O(N)` time. | ||
- Therefore, the overall time complexity is `O(N)`. | ||
- **Space Complexity: O(N)** | ||
- We use two additional arrays `maxLeft` and `maxRight`, each of size `N`. | ||
- Hence, the space complexity is `O(N)`. | ||
|
||
## Code | ||
|
||
- [Java](../src/main/java/io/dksifoua/leetcode/trappingrainwater/Solution.java) |
29 changes: 29 additions & 0 deletions
29
src/main/java/io/dksifoua/leetcode/trappingrainwater/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.dksifoua.leetcode.trappingrainwater; | ||
|
||
public class Solution { | ||
|
||
public int trap(int[] height) { | ||
int[] maxLeft = new int[height.length]; | ||
int[] maxRight = new int[height.length]; | ||
|
||
for (int i = 0, j = height.length - 1; i < height.length && j >= 0; i++, j--) { | ||
if (i == 0 && j == height.length - 1) { | ||
maxLeft[i] = height[i]; | ||
maxRight[j] = height[j]; | ||
continue; | ||
} | ||
|
||
maxLeft[i] = Math.max(maxLeft[i - 1], height[i]); | ||
maxRight[j] = Math.max(height[j], maxRight[j + 1]); | ||
} | ||
|
||
int result = 0; | ||
for (int i = 0; i < height.length; i++) { | ||
if (maxLeft[i] > height[i] && maxRight[i] > height[i]) { | ||
result += Math.min(maxLeft[i], maxRight[i]) - height[i]; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/io/dksifoua/leetcode/trappingrainwater/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.dksifoua.leetcode.trappingrainwater; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SolutionTest { | ||
|
||
private final Solution solution = new Solution(); | ||
|
||
@Test | ||
void test1() { | ||
int[] height = new int[] { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 }; | ||
Assertions.assertEquals(6, solution.trap(height)); | ||
} | ||
|
||
@Test | ||
void test2() { | ||
int[] height = new int[] { 4, 2, 0, 3, 2, 5 }; | ||
Assertions.assertEquals(9, solution.trap(height)); | ||
} | ||
} |