Skip to content

Commit 18c6fb2

Browse files
committed
装最多水的容器
1 parent 407ec1b commit 18c6fb2

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

container_with_most_water.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param heights: a list of integers
5+
# @return: an integer
6+
def maxArea(self, heights):
7+
# write your code here
8+
ret = 0
9+
begin, end = 0, len(heights) - 1
10+
while begin < end:
11+
vol = min(heights[begin], heights[end]) * (end - begin)
12+
if vol > ret:
13+
ret = vol
14+
if heights[begin] > heights[end]:
15+
# 右边向左移动直到heights[pos] > height[end],只有这样,下一次结果才有可能比当前更大。
16+
pos = end - 1
17+
while (pos >= begin) and (heights[pos] <= heights[end]):
18+
pos -= 1
19+
end = pos
20+
else:
21+
# 左边向右移动直到heights[pos] > height[begin]
22+
pos = begin + 1
23+
while (pos <= end) and (heights[pos] <= heights[begin]):
24+
pos += 1
25+
begin = pos
26+
return ret

0 commit comments

Comments
 (0)