-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
11.py
104 lines (102 loc) · 3.6 KB
/
11.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
99
100
101
102
103
104
__________________________________________________________________________________________________
48ms
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = area = 0
left, right = 0, len(height) - 1
while left < right:
l, r = height[left], height[right]
if l < r:
area = (right - left) * l
#print(str(area)+"area")
while height[left] <= l: #Iterate till u find a better left. i.e higher than l.
left += 1
#print("left:"+str(left))
else:
area = (right - left) * r
#print("area"+str(area)+"area")
while height[right] <= r and right:
right -= 1
#print("right:"+str(right)+" "+str(height[right])+" "+str(r) )
if area > max_area:
max_area = area
return max_area
""" slower code"""
max_A = 0
l = 0
r = len(height) - 1
while l < r:
max_A = max(max_A, min(height[r],height[l])*(r-l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return max_A
__________________________________________________________________________________________________
52ms
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
left, right = 0, len(height) - 1
while left < right:
l, r = height[left], height[right]
if l < r:
area = (right - left) * l
while height[left] <= l:
left += 1
else:
area = (right - left) * r
while height[right] <= r and right:
right -= 1
if area > max_area:
max_area = area
return max_area
__________________________________________________________________________________________________
56ms
class Solution:
def maxArea(self, height: List[int]) -> int:
ans = 0
l, r = iter(height), iter(height[::-1])
hl, hr = next(l), next(r)
for d in range(len(height) - 1, 0, -1):
if hl < hr:
ans = max(ans, hl * d)
hl = next(l)
else:
ans = max(ans, hr * d)
hr = next(r)
return ans
__________________________________________________________________________________________________
13280 kb
class Solution:
def maxArea(self, height: 'List[int]') -> 'int':
arr_len = len(height)
area = 0
front = 0
rear = arr_len - 1
while front < rear:
if height[front] < height[rear]:
h = height[front]
front += 1
else:
h = height[rear]
rear -= 1
new_area = h * (arr_len - 1)
area = max(area, new_area)
arr_len -= 1
return area
__________________________________________________________________________________________________
13308 kb
class Solution:
def maxArea(self, height: 'List[int]') -> 'int':
max_num=0
for i in range(len(height)):
max_num = max(min(height[0], height[-1])*(len(height)-1), max_num)
if len(height)>2:
if height[0]>=height[-1]:
del height[-1]
else:
del height[0]
else:
return max_num
__________________________________________________________________________________________________