Skip to content

Commit dfc2250

Browse files
authored
[Two Pointers][Container With Most Water] - Implement solution for Leetcode 11 (#18)
1 parent 8efbf8a commit dfc2250

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package containerwithmostwater
2+
3+
// Time complexity: O(n^2)
4+
// Space complexity: O(1)
5+
func MaxAreaBruteForce(heights []int) int {
6+
n := len(heights)
7+
maxArea := 0
8+
9+
for p1 := 0; p1 < n; p1++ {
10+
for p2 := p1 + 1; p2 < n; p2++ {
11+
height := heights[p1]
12+
if heights[p2] < height {
13+
height = heights[p2]
14+
}
15+
width := p2 - p1
16+
area := height * width
17+
18+
if area > maxArea {
19+
maxArea = area
20+
}
21+
22+
// height := min(heights[p1], heights[p2])
23+
// width := p2 - p1
24+
// area := height * width
25+
// maxArea = max(maxArea, area)
26+
}
27+
}
28+
return maxArea
29+
}
30+
31+
// Time complexity: O(n)
32+
// Space complexity: O(1)
33+
func MaxAreaTwoPointers(heights []int) int {
34+
p1, p2, maxArea := 0, len(heights)-1, 0
35+
36+
for p1 < p2 {
37+
height := heights[p1]
38+
if heights[p2] < height {
39+
height = heights[p2]
40+
}
41+
width := p2 - p1
42+
area := height * width
43+
44+
if area > maxArea {
45+
maxArea = area
46+
}
47+
48+
// height := min(heights[p1], heights[p2])
49+
// width := p2 - p1
50+
// area := height * width
51+
// maxArea = max(maxArea, area)
52+
53+
if heights[p1] < heights[p2] {
54+
p1++
55+
} else {
56+
p2--
57+
}
58+
}
59+
return maxArea
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package containerwithmostwater
2+
3+
import "testing"
4+
5+
func TestMaxArea(t *testing.T) {
6+
cases := []struct {
7+
name string
8+
fn func([]int) int
9+
heights []int
10+
expected int
11+
}{
12+
// MaxAreaBruteForce
13+
{"BruteForce - Example 1", MaxAreaBruteForce, []int{1, 7, 2, 5, 4, 7, 3, 6}, 36},
14+
{"BruteForce - Example 2", MaxAreaBruteForce, []int{2, 2, 2}, 4},
15+
{"BruteForce - Minimal input size 2", MaxAreaBruteForce, []int{1, 1}, 1},
16+
{"BruteForce - All zeroes", MaxAreaBruteForce, []int{0, 0, 0, 0}, 0},
17+
{"BruteForce - Increasing heights", MaxAreaBruteForce, []int{1, 2, 3, 4, 5}, 6},
18+
{"BruteForce - Decreasing heights", MaxAreaBruteForce, []int{5, 4, 3, 2, 1}, 6},
19+
20+
// MaxAreaTwoPointers
21+
{"TwoPointers - Example 1", MaxAreaTwoPointers, []int{1, 7, 2, 5, 4, 7, 3, 6}, 36},
22+
{"TwoPointers - Example 2", MaxAreaTwoPointers, []int{2, 2, 2}, 4},
23+
{"TwoPointers - Minimal input size 2", MaxAreaTwoPointers, []int{1, 1}, 1},
24+
{"TwoPointers - All zeroes", MaxAreaTwoPointers, []int{0, 0, 0, 0}, 0},
25+
{"TwoPointers - Increasing heights", MaxAreaTwoPointers, []int{1, 2, 3, 4, 5}, 6},
26+
{"TwoPointers - Decreasing heights", MaxAreaTwoPointers, []int{5, 4, 3, 2, 1}, 6},
27+
}
28+
29+
for _, c := range cases {
30+
t.Run(c.name, func(t *testing.T) {
31+
got := c.fn(c.heights)
32+
if got != c.expected {
33+
t.Errorf("%s failed: expected %v, got %v", c.name, c.expected, got)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)