Skip to content

Commit

Permalink
Resolved LC11
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanData committed Oct 10, 2023
1 parent 0e2a0da commit 3217a8a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lc/lc11ContainerWithMostWater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lc

import (
"testing"
)

func maxArea(height []int) int {
maxArea := 0
left, right := 0, len(height)-1
for left < right {
currentArea := (right - left) * min(height[left], height[right])
maxArea = max(currentArea, maxArea)
if height[left] < height[right] {
left++
} else {
right--
}
}
return maxArea
}

func TestMaxArea(t *testing.T) {
tests := []struct {
height []int
want int
}{
{[]int{1, 8, 6, 2, 5, 4, 8, 3, 7}, 49},
{[]int{1, 1}, 1},
}

for _, test := range tests {
got := maxArea(test.height)
if got != test.want {
t.Errorf("maxArea(%v) = %v; want %v", test.height, got, test.want)
}
}
}
8 changes: 8 additions & 0 deletions lc/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ func max[T constraints.Ordered](a, b T) T {
return b
}
}

func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
} else {
return b
}
}
21 changes: 21 additions & 0 deletions py/11.ContainerWithMostWater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List


class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea = 0
left, right = 0, len(height) - 1
while left < right:
currentArea = (right - left) * min(height[left], height[right])
maxArea = max(currentArea, maxArea)
if height[left] < height[right]:
left += 1
else:
right -= 1
return maxArea


if __name__ == '__main__':
sol = Solution()
assert sol.maxArea(height=[1, 8, 6, 2, 5, 4, 8, 3, 7]) == 49
assert sol.maxArea(height=[1, 1]) == 1

0 comments on commit 3217a8a

Please sign in to comment.