Skip to content

Commit 7baa3cd

Browse files
committed
Trapping Rain Water
1 parent daa1fbb commit 7baa3cd

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

297.serialise_and_desirialise_binary_tree/tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 297. Serialize and Deserialize Binary Tree
22
// Topics: 'String', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Tree'
3-
// Level: 'Medium'
3+
// Level: 'Hard'
44

55
// Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
66

42.trapping_rain_water/rain.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 42. Trapping Rain Water
2+
// Topics: 'Array', 'Two Pointers', 'Dynamic Programming', 'Stack', 'Monotonic Stack'
3+
// Level: 'Hard'
4+
5+
// Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
6+
7+
// Example 1:
8+
9+
// Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
10+
// Output: 6
11+
// Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
12+
13+
// Example 2:
14+
15+
// Input: height = [4,2,0,3,2,5]
16+
// Output: 9
17+
18+
// Constraints:
19+
20+
// n == height.length
21+
// 1 <= n <= 2 * 104
22+
// 0 <= height[i] <= 105
23+
24+
package trappingrainwater
25+
26+
func trap(height []int) int {
27+
if len(height) < 2 {
28+
return 0
29+
}
30+
R := len(height) - 1
31+
L := 0
32+
var sum int
33+
34+
maxr, maxl := R-1, L+1
35+
for L < R {
36+
if height[L] < height[R] {
37+
var ocuppied int
38+
for height[maxl] < height[L] && maxl != R {
39+
ocuppied += height[maxl]
40+
maxl++
41+
}
42+
sum += min(height[L], height[maxl]) * (maxl - L - 1)
43+
sum -= ocuppied
44+
L = maxl
45+
maxl++
46+
} else {
47+
var occupied int
48+
for height[maxr] < height[R] && maxr != L {
49+
occupied += height[maxr]
50+
maxr--
51+
}
52+
sum += min(height[R], height[maxr]) * (R - maxr - 1)
53+
sum -= occupied
54+
R = maxr
55+
maxr--
56+
}
57+
}
58+
return sum
59+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package trappingrainwater
2+
3+
import "testing"
4+
5+
func TestTrap(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
height []int
9+
want int
10+
}{
11+
{
12+
name: "example 0",
13+
height: []int{5, 4, 1, 2},
14+
want: 1,
15+
},
16+
{
17+
name: "example 1",
18+
height: []int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1},
19+
want: 6,
20+
},
21+
{
22+
name: "example 2",
23+
height: []int{4, 2, 0, 3, 2, 5},
24+
want: 9,
25+
},
26+
{
27+
name: "no water trapped - flat",
28+
height: []int{0, 0, 0, 0},
29+
want: 0,
30+
},
31+
{
32+
name: "no water trapped - ascending",
33+
height: []int{1, 2, 3, 4, 5},
34+
want: 0,
35+
},
36+
{
37+
name: "no water trapped - descending",
38+
height: []int{5, 4, 3, 2, 1},
39+
want: 0,
40+
},
41+
{
42+
name: "single element",
43+
height: []int{5},
44+
want: 0,
45+
},
46+
{
47+
name: "two elements",
48+
height: []int{3, 0},
49+
want: 0,
50+
},
51+
{
52+
name: "simple valley",
53+
height: []int{3, 0, 3},
54+
want: 3,
55+
},
56+
{
57+
name: "multiple valleys",
58+
height: []int{3, 0, 2, 0, 4},
59+
want: 7,
60+
},
61+
{
62+
name: "complex pattern",
63+
height: []int{5, 2, 1, 2, 1, 5},
64+
want: 14,
65+
},
66+
{
67+
name: "all zeros",
68+
height: []int{0, 0, 0},
69+
want: 0,
70+
},
71+
{
72+
name: "peak in middle",
73+
height: []int{2, 0, 2},
74+
want: 2,
75+
},
76+
{
77+
name: "asymmetric walls",
78+
height: []int{5, 1, 3},
79+
want: 2,
80+
},
81+
{
82+
name: "long flat section",
83+
height: []int{3, 0, 0, 6, 0, 0, 3},
84+
want: 12,
85+
},
86+
{
87+
name: "water at different levels",
88+
height: []int{4, 2, 3},
89+
want: 1,
90+
},
91+
}
92+
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
got := trap(tt.height)
96+
if got != tt.want {
97+
t.Errorf("trap(%v) = %v, want %v", tt.height, got, tt.want)
98+
}
99+
})
100+
}
101+
}

0 commit comments

Comments
 (0)