1 parent 407ec1b commit 18c6fb2Copy full SHA for 18c6fb2
1 file changed
container_with_most_water.py
@@ -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