Skip to content

Commit 71bdf2d

Browse files
author
Shuo
authored
Merge pull request #587 from openset/develop
Add: Container With Most Water
2 parents d4001ff + 84c4a7f commit 71bdf2d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
package container_with_most_water
2+
3+
func maxArea(height []int) int {
4+
ans, l, r := 0, 0, len(height)-1
5+
for l < r {
6+
w, h := r-l, height[l]
7+
if h < height[r] {
8+
l++
9+
} else {
10+
h, r = height[r], r-1
11+
}
12+
if area := w * h; area > ans {
13+
ans = area
14+
}
15+
}
16+
return ans
17+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package container_with_most_water
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected int
8+
}
9+
10+
func TestMaxArea(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 8, 6, 2, 5, 4, 8, 3, 7},
14+
expected: 49,
15+
},
16+
{
17+
input: []int{1, 8, 6, 30, 20, 6, 9, 10, 1},
18+
expected: 48,
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := maxArea(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)