Skip to content

Commit

Permalink
Trapping Rain Water
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed Jul 11, 2024
1 parent 4a5818b commit 799d745
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
![Makefile CI](https://github.com/dksifoua/leetcode/actions/workflows/makefile-ci.yaml/badge.svg)
![Codecov](https://img.shields.io/codecov/c/github/dksifoua/leetcode)

| ID | Difficulty | Problem | Topics | Link |
|------|------------|------------------------------------|-----------------------------------|------------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0011 | Medium | Container With Most Water | Array, Two Pointers, Greedy | [solution](./docs/0011-Container-With-Most-Water.md) |
| 0015 | Medium | Three sum | Array, Two Pointers, Sorting | [solution](./docs/0015-Three-Sum.md) |
| 0036 | Medium | Valid Sudoku | Array, HashTable, Matrix | [solution](./docs/0036-Valid-Sudoku.md) |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0128 | Medium | Longest Consecutive Sequence | Array, HashSet | [solution](./docs/0128-Longest-Consecutive-Sequence.md) |
| 0167 | Medium | Two Sum II - Input Array Is Sorted | Array, Two Pointers | [solution](./docs/0167-Two-Sum-II-Array-Is-Sorted.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0238 | Medium | Product of Array Except Self | Array | [solution](./docs/0238-Product-Of-Array-Except-Self.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
| ID | Difficulty | Problem | Topics | Link |
|------|------------|------------------------------------|-------------------------------------------------|------------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0011 | Medium | Container With Most Water | Array, Two Pointers, Greedy | [solution](./docs/0011-Container-With-Most-Water.md) |
| 0015 | Medium | Three sum | Array, Two Pointers, Sorting | [solution](./docs/0015-Three-Sum.md) |
| 0036 | Medium | Valid Sudoku | Array, HashTable, Matrix | [solution](./docs/0036-Valid-Sudoku.md) |
| 0042 | Hard | Trapping Rain Water | Array, Two Pointers, Dynamic Programming, Stack | [solution](./docs/0042-Trapping-Rain-Water.md) |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0128 | Medium | Longest Consecutive Sequence | Array, HashSet | [solution](./docs/0128-Longest-Consecutive-Sequence.md) |
| 0167 | Medium | Two Sum II - Input Array Is Sorted | Array, Two Pointers | [solution](./docs/0167-Two-Sum-II-Array-Is-Sorted.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0238 | Medium | Product of Array Except Self | Array | [solution](./docs/0238-Product-Of-Array-Except-Self.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
40 changes: 40 additions & 0 deletions docs/0042-Trapping-Rain-Water.md
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 src/main/java/io/dksifoua/leetcode/trappingrainwater/Solution.java
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;
}
}
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));
}
}

0 comments on commit 799d745

Please sign in to comment.