Skip to content

Commit 6218eac

Browse files
authored
Merge pull request #46 from harisdev-netizen/harisdev-branch
"added 0005 Trapping Rain Water to L-A"
2 parents dcd1321 + 8878a7f commit 6218eac

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 0005 Trapping Rain Water ( L-A )
2+
3+
## Problem
4+
5+
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
6+
7+
## Example 1
8+
9+
```
10+
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
11+
Output: 6
12+
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
13+
```
14+
15+
## Solution
16+
17+
```javascript
18+
const trap = function (height) {
19+
let ans = 0;
20+
const size = height.length;
21+
let leftMax = height[0];
22+
let rightMax = height[size - 1];
23+
const leftArr = new Array(size);
24+
leftArr[0] = height[0];
25+
const rightArr = new Array(size);
26+
rightArr[size - 1] = height[size - 1];
27+
28+
for (let i = 1; i < size; i++) {
29+
leftMax = Math.max(leftMax, height[i]);
30+
leftArr[i] = leftMax;
31+
}
32+
33+
for (let k = size - 2; k >= 0; k--) {
34+
rightMax = Math.max(rightMax, height[k]);
35+
rightArr[k] = rightMax;
36+
}
37+
38+
for (let j = 0; j < size; j++) {
39+
ans += Math.max(Math.min(leftArr[j], rightArr[j]) - height[j], 0);
40+
}
41+
42+
return ans;
43+
};
44+
45+
```
46+
47+
## How it works
48+
49+
- The function `trap` takes an array of integers `height` as input and returns an integer representing the amount of water that can be trapped.
50+
- The function initializes the variables `ans`, `size`, `leftMax`, and `rightMax` to 0, the length of the `height` array, and the maximum heights to the left and right of the array, respectively. It also initializes two arrays leftArr and rightArr to store the maximum height to the left and right of each index, respectively.
51+
- The function then loops through the `height` array from left to right and stores the maximum height to the left of each index in the `leftArr` array using the variable `leftMax`.
52+
- The function loops through the `height` array from right to left and stores the maximum height to the right of each index in the `rightArr` array using the variable `rightMax`.
53+
- The function loops through the `height` array again and calculates the amount of water that can be trapped at each index by taking the minimum of the maximum heights to the left and right of the index, subtracting the height at the index, and taking the maximum with 0 to avoid negative values. The calculated value is added to the `ans` variable.
54+
- The function returns the final `ans` value.
55+
56+
## References
57+
58+
- [LeetCode](https://leetcode.com/problems/trapping-rain-water/)
59+
60+
## Problem Added By
61+
62+
- [Haris](https://github.com/harisdev-netizen)
63+
64+
## Contributing
65+
66+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
67+
68+
Please make sure to update tests as appropriate.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[]} height - Array of heights.
3+
* @return {number} - Amount of water that can be trapped.
4+
*/
5+
6+
const trap = function (height) {
7+
let ans = 0;
8+
const size = height.length;
9+
let leftMax = height[0];
10+
let rightMax = height[size - 1];
11+
const leftArr = new Array(size);
12+
leftArr[0] = height[0];
13+
const rightArr = new Array(size);
14+
rightArr[size - 1] = height[size - 1];
15+
16+
// Calculate the maximum height to the left of each index and store it in the leftArr.
17+
for (let i = 1; i < size; i++) {
18+
leftMax = Math.max(leftMax, height[i]);
19+
leftArr[i] = leftMax;
20+
}
21+
22+
// Calculate the maximum height to the right of each index and store it in the rightArr.
23+
for (let k = size - 2; k >= 0; k--) {
24+
rightMax = Math.max(rightMax, height[k]);
25+
rightArr[k] = rightMax;
26+
}
27+
28+
// Calculate the amount of water that can be trapped at each index and add it to the answer.
29+
for (let j = 0; j < size; j++) {
30+
ans += Math.max(Math.min(leftArr[j], rightArr[j]) - height[j], 0);
31+
}
32+
33+
return ans;
34+
};

0 commit comments

Comments
 (0)