-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path011_containerWithMostWater.py
59 lines (48 loc) · 1.5 KB
/
011_containerWithMostWater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#-*-coding:utf-8-*-
__author__ = 'Rye'
class Solution:
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
# 应用贪心算法
# https://github.com/apachecn/awesome-algorithm/blob/master/docs/Leetcode_Solutions/Python/011._container_with_most_water.md
left = 0
right = len(height) - 1
maxArea = 0
while left < right:
if height[left] < height[right]:
Area = height[left] * (right - left)
if maxArea < Area:
maxArea = Area
left += 1
else:
Area = height[right] * (right - left)
if maxArea < Area:
maxArea = Area
right -= 1
return maxArea
# 超时
class Solution1:
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
maxArea = 0
length = len(height)
smaller = 0
for i in range(length):
for j in range(i + 1, length):
if height[i] <= height[j]:
smaller = height[i]
else:
smaller = height[j]
Area = smaller * (j - i)
if maxArea < Area:
maxArea = Area
return maxArea
if __name__ == '__main__':
num = [1, 8, 6, 2, 5, 4, 8, 3, 7] # 输出: 49
print(Solution().maxArea(num))