原题
https://leetcode.cn/problems/container-with-most-water/description/
思路
双指针
复杂度
时间:O(n)
空间:O(1)
Python代码
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
w = r - l
h = min(height[l], height[r])
area = w * h
ans = max(ans, area)
if height[l] < height[r]:
l += 1
else:
r -= 1
return ans
Go代码
func maxArea(height []int) int {
l, r := 0, len(height)-1
ans := 0
for l < r {
w := r - l
h := min(height[l], height[r])
area := w * h
ans = max(ans, area)
// 移动指针
if height[l] < height[r] {
l++
} else {
r--
}
}
return ans
}
4954

被折叠的 条评论
为什么被折叠?



