Skip to content

Commit e9d1f3c

Browse files
authored
Implement Trapped Rain Water algorithm. (#714)
* feat: implement Trapped Rain Water algorithm * chore(tests): add test of empty `heights`
1 parent cbaed23 commit e9d1f3c

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
314314
17. [`Max`](./dynamic/knapsack.go#L11): Max function - possible duplicate
315315
18. [`NthCatalanNumber`](./dynamic/catalan.go#L13): NthCatalan returns the n-th Catalan Number Complexity: O(n²)
316316
19. [`NthFibonacci`](./dynamic/fibonacci.go#L6): NthFibonacci returns the nth Fibonacci Number
317-
317+
20. [`TrapRainWater`](./dynamic/traprainwater.go#L17): Calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
318318
---
319319
</details><details>
320320
<summary> <strong> dynamicarray </strong> </summary>

dynamic/traprainwater.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// filename: traprainwater.go
2+
// description: Provides a function to calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
3+
// details:
4+
// The TrapRainWater function calculates the amount of trapped rainwater between the bars represented by the given elevation map.
5+
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
6+
// Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights.
7+
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
8+
// author(s) [TruongNhanNguyen (SOZEL)](https://github.com/TruongNhanNguyen)
9+
package dynamic
10+
11+
import "math"
12+
13+
// TrapRainWater calculates the amount of trapped rainwater between the bars represented by the given elevation map.
14+
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
15+
// Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights.
16+
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
17+
func TrapRainWater(height []int) int {
18+
if len(height) == 0 {
19+
return 0
20+
}
21+
22+
leftMax := make([]int, len(height))
23+
rightMax := make([]int, len(height))
24+
25+
leftMax[0] = height[0]
26+
for i := 1; i < len(height); i++ {
27+
leftMax[i] = int(math.Max(float64(leftMax[i-1]), float64(height[i])))
28+
}
29+
30+
rightMax[len(height)-1] = height[len(height)-1]
31+
for i := len(height) - 2; i >= 0; i-- {
32+
rightMax[i] = int(math.Max(float64(rightMax[i+1]), float64(height[i])))
33+
}
34+
35+
trappedWater := 0
36+
for i := 0; i < len(height); i++ {
37+
trappedWater += int(math.Min(float64(leftMax[i]), float64(rightMax[i]))) - height[i]
38+
}
39+
40+
return trappedWater
41+
}

dynamic/traprainwater_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dynamic_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/TheAlgorithms/Go/dynamic"
8+
)
9+
10+
func TestTrapRainWater(t *testing.T) {
11+
heights := [][]int{
12+
{},
13+
{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1},
14+
{4, 2, 0, 3, 2, 5},
15+
{3, 1, 2, 4, 0, 1, 3, 2, 4},
16+
}
17+
18+
expectedResults := []int{
19+
0,
20+
6,
21+
9,
22+
13,
23+
}
24+
25+
for i, height := range heights {
26+
expected := expectedResults[i]
27+
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
28+
result := dynamic.TrapRainWater(height)
29+
if result != expected {
30+
t.Errorf("Expected %d, but got %d", expected, result)
31+
}
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)