Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions problems/container-with-most-water/container_with_most_water.go
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
package container_with_most_water

func maxArea(height []int) int {
ans, l, r := 0, 0, len(height)-1
for l < r {
w, h := r-l, height[l]
if h < height[r] {
l++
} else {
h, r = height[r], r-1
}
if area := w * h; area > ans {
ans = area
}
}
return ans
}
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
package container_with_most_water

import "testing"

type caseType struct {
input []int
expected int
}

func TestMaxArea(t *testing.T) {
tests := [...]caseType{
{
input: []int{1, 8, 6, 2, 5, 4, 8, 3, 7},
expected: 49,
},
{
input: []int{1, 8, 6, 30, 20, 6, 9, 10, 1},
expected: 48,
},
}
for _, tc := range tests {
output := maxArea(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}