feat: monday
This commit is contained in:
31
pkg/trapping_rain_water/main.go
Normal file
31
pkg/trapping_rain_water/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package trapping_rain_water
|
||||
|
||||
func Trap(heights []int) int {
|
||||
// Calculate area taken up by the boundary.
|
||||
boundary_area := 0
|
||||
for _, height := range heights {
|
||||
boundary_area += height
|
||||
}
|
||||
|
||||
// Calculate the total area taken up by the boundary and the water.
|
||||
total_area := 0
|
||||
left := 0
|
||||
left_height := 0
|
||||
right := len(heights) - 1
|
||||
right_height := 0
|
||||
|
||||
for left <= right {
|
||||
left_height = max(left_height, heights[left])
|
||||
right_height = max(right_height, heights[right])
|
||||
|
||||
if left_height < right_height {
|
||||
total_area += left_height
|
||||
left++
|
||||
} else {
|
||||
total_area += right_height
|
||||
right--
|
||||
}
|
||||
}
|
||||
|
||||
return total_area - boundary_area
|
||||
}
|
||||
Reference in New Issue
Block a user