Leetcode-11

Question

Solution

建立两个指针,从左右向中间遍历。用宽度来循环。

1
2
3
4
5
6
7
8
9
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r, width, res = 0, len(height) - 1, len(height) - 1, 0
for w in range(width, 0, -1):
if height[l] < height[r]:
l, res = l + 1, max(res, height[l] * w)
else:
r, res = r - 1, max(res, height[r] * w)
return res