Skip to content

Implement Trapped Rain Water algorithm. #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
17. [`Max`](./dynamic/knapsack.go#L11): Max function - possible duplicate
18. [`NthCatalanNumber`](./dynamic/catalan.go#L13): NthCatalan returns the n-th Catalan Number Complexity: O(n²)
19. [`NthFibonacci`](./dynamic/fibonacci.go#L6): NthFibonacci returns the nth Fibonacci Number

20. [`TrapRainWater`](./dynamic/traprainwater.go#L17): Calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
---
</details><details>
<summary> <strong> dynamicarray </strong> </summary>
Expand Down
41 changes: 41 additions & 0 deletions dynamic/traprainwater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// filename: traprainwater.go
// description: Provides a function to calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
// details:
// The TrapRainWater function calculates the amount of trapped rainwater between the bars represented by the given elevation map.
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
// 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.
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
// author(s) [TruongNhanNguyen (SOZEL)](https://github.com/TruongNhanNguyen)
package dynamic

import "math"

// TrapRainWater calculates the amount of trapped rainwater between the bars represented by the given elevation map.
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
// 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.
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
func TrapRainWater(height []int) int {
if len(height) == 0 {
return 0
}

leftMax := make([]int, len(height))
rightMax := make([]int, len(height))

leftMax[0] = height[0]
for i := 1; i < len(height); i++ {
leftMax[i] = int(math.Max(float64(leftMax[i-1]), float64(height[i])))
}

rightMax[len(height)-1] = height[len(height)-1]
for i := len(height) - 2; i >= 0; i-- {
rightMax[i] = int(math.Max(float64(rightMax[i+1]), float64(height[i])))
}

trappedWater := 0
for i := 0; i < len(height); i++ {
trappedWater += int(math.Min(float64(leftMax[i]), float64(rightMax[i]))) - height[i]
}

return trappedWater
}
34 changes: 34 additions & 0 deletions dynamic/traprainwater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dynamic_test

import (
"fmt"
"testing"

"github.com/TheAlgorithms/Go/dynamic"
)

func TestTrapRainWater(t *testing.T) {
heights := [][]int{
{},
{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1},
{4, 2, 0, 3, 2, 5},
{3, 1, 2, 4, 0, 1, 3, 2, 4},
}

expectedResults := []int{
0,
6,
9,
13,
}

for i, height := range heights {
expected := expectedResults[i]
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
result := dynamic.TrapRainWater(height)
if result != expected {
t.Errorf("Expected %d, but got %d", expected, result)
}
})
}
}
Loading