Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 11 ms (45.67%), Memory - 6.3 MB (1…
Browse files Browse the repository at this point in the history
…6.84%)
  • Loading branch information
hucancode committed Apr 12, 2024
1 parent 4cdc464 commit 6d723c6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions leetcode/0042-trapping-rain-water/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func trap(height []int) int {
n := len(height)
left := make([]int, n)
right := make([]int, n)
left[0] = height[0]
right[n-1] = height[n-1]
for i := 1;i<n;i++ {
left[i] = max(left[i-1], height[i])
}
for i := n-2;i>=0;i-- {
right[i] = max(right[i+1], height[i])
}
ret := 0
for i := 0;i<n;i++ {
ret += min(left[i], right[i]) - height[i]
}
return ret
}

0 comments on commit 6d723c6

Please sign in to comment.