Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3271ee9

Browse files
committed
Add Rain Terraces problem.
1 parent f142ca0 commit 3271ee9

File tree

1 file changed

+32
-8
lines changed
  • src/algorithms/uncategorized/rain-terraces

1 file changed

+32
-8
lines changed

‎src/algorithms/uncategorized/rain-terraces/README.md‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Rain Terraces (Trapping Rain Water) Problem
22

3-
Given an array of non-negative integers representing terraces in an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
3+
Given an array of non-negative integers representing terraces in an elevation map
4+
where the width of each bar is `1`, compute how much water it is able to trap
5+
after raining.
46

57
![Rain Terraces](https://www.geeksforgeeks.org/wp-content/uploads/watertrap.png)
68

@@ -9,44 +11,66 @@ Given an array of non-negative integers representing terraces in an elevation ma
911
**Example #1**
1012

1113
```
12-
Input: arr[] = [2, 0, 2]
14+
Input: arr[] = [2, 0, 2]
1315
Output: 2
14-
Structure is like below
16+
Structure is like below:
17+
1518
| |
1619
|_|
20+
1721
We can trap 2 units of water in the middle gap.
1822
```
1923

2024
**Example #2**
2125

2226
```
23-
Input: arr[] = [3, 0, 0, 2, 0, 4]
27+
Input: arr[] = [3, 0, 0, 2, 0, 4]
2428
Output: 10
25-
Structure is like below
29+
Structure is like below:
30+
2631
|
2732
| |
2833
| | |
2934
|__|_|
35+
3036
We can trap "3*2 units" of water between 3 an 2,
3137
"1 unit" on top of bar 2 and "3 units" between 2
32-
and 4. See below diagram also.
38+
and 4. See below diagram also.
3339
```
3440

3541
**Example #3**
3642

3743
```
3844
Input: arr[] = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
3945
Output: 6
46+
Structure is like below:
47+
4048
|
4149
| || |
4250
_|_||_||||||
51+
4352
Trap "1 unit" between first 1 and 2, "4 units" between
44-
first 2 and 3 and "1 unit" between second last 1 and last 2
53+
first 2 and 3 and "1 unit" between second last 1 and last 2.
4554
```
4655

47-
## Algorithms
56+
## The Algorithm
57+
58+
An element of array can store water if there are higher bars on left and right.
59+
We can find amount of water to be stored in every element by finding the heights
60+
of bars on left and right sides. The idea is to compute amount of water that can
61+
be stored in every element of array. For example, consider the array
62+
`[3, 0, 0, 2, 0, 4]`, We can trap "3*2 units" of water between 3 an 2, "1 unit"
63+
on top of bar 2 and "3 units" between 2 and 4. See below diagram also.
4864

65+
A **simple solution** is to traverse every array element and find the highest
66+
bars on left and right sides. Take the smaller of two heights. The difference
67+
between smaller height and height of current element is the amount of water
68+
that can be stored in this array element. Time complexity of this solution
69+
is `O(n2)`.
4970

71+
An **efficient solution** is to pre-compute highest bar on left and right of
72+
every bar in `O(n)` time. Then use these pre-computed values to find the
73+
amount of water in every array element.
5074

5175
## References
5276

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /