MCPcopy Create free account
hub / github.com/VanjaRo/LeetCode / trap

Function trap

tasks/42.go:9–35  ·  view source on GitHub ↗
(height []int)

Source from the content-addressed store, hash-verified

7}
8
9func trap(height []int) int {
10 left := 0
11 right := len(height) - 1
12
13 ret := 0
14
15 leftMax := 0
16 rightMax := 0
17 for left < right {
18 if height[left] < height[right] {
19 if height[left] > leftMax {
20 leftMax = height[left]
21 } else {
22 ret += leftMax - height[left]
23 }
24 left++
25 } else {
26 if height[right] > rightMax {
27 rightMax = height[right]
28 } else {
29 ret += rightMax - height[right]
30 }
31 right--
32 }
33 }
34 return ret
35}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected