-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11. Container With Most Water.py
107 lines (91 loc) · 2.78 KB
/
11. Container With Most Water.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# O(n), accepted, (Two Pointer Approach, code simplified)
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_vol = 0
length = len(height)
left_index = 0
right_index = length - 1
while left_index <= right_index:
max_vol = max(max_vol,(min(height[left_index],height[right_index])*(right_index-left_index)))
if height[left_index] >= height[right_index]:
right_index = right_index - 1
else:
left_index = left_index + 1
return max_vol
# O(n), accepted, (Two Pointer Approach)
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_vol = 0
length = len(height)
left_index = 0
right_index = length - 1
left = height[left_index]
right = height[right_index]
while left_index <= right_index:
max_vol = max(max_vol,(min(left,right)*(right_index-left_index)))
if left >= right:
right_index = right_index - 1
right = height[right_index]
else:
left_index = left_index + 1
left = height[left_index]
return max_vol
# O(n^2) with max_left & max_right optimisation, still too slow
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_vol = 0
max_left = 0
length = len(height)
for i in range(0,length-1):
if height[i] > max_left:
max_left = height[i]
max_right = 0
for j in reversed(range(i,length)):
if height[j] > max_right:
max_right = height[j]
temp_vol = min(height[i],height[j])*(j-i)
max_vol = max(max_vol,temp_vol)
return max_vol
# O(n^2) with max_left optimisation, still too slow
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_vol = 0
max_left = 0
length = len(height)
for i in range(0,length-1):
if height[i] > max_left:
max_left = height[i]
for j in range(i,length):
temp_vol = min(height[i],height[j])*(j-i)
max_vol = max(max_vol,temp_vol)
return max_vol
# O(n^2), too slow
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_vol = 0
length = len(height)
for i in range(0,length-1):
for j in range(i,length):
temp_vol = min(height[i],height[j])*(j-i)
max_vol = max(max_vol,temp_vol)
return max_vol