Skip to content

Commit 740f160

Browse files
committed
no message
1 parent 54dd56f commit 740f160

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Time: O(N)
3+
Space: O(1)
4+
5+
i and j starts from the leftest and rightest. Move the one that has less height.
6+
7+
Why we won't miss any i and j?
8+
For example, currently i is heigher than j.
9+
If between i~j, there are no height that is larger or equal to i, than since the area is `min(height[i], height[j]) * (j-i)`, you cannot find any area that is larger than the current one.
10+
If between i~j, there is a height that is larger or equal to i, j will on it, and it will be tested.
11+
Thus, given any i and j, any other future i and j that have the potential of forming larger area will be tested.
12+
"""
13+
class Solution:
14+
def maxArea(self, height: List[int]) -> int:
15+
i = 0
16+
j = len(height)-1
17+
ans = 0
18+
19+
while i<j:
20+
ans = max(ans, min(height[i], height[j]) * (j-i))
21+
if height[i]>height[j]:
22+
j -= 1
23+
else:
24+
i += 1
25+
return ans

0 commit comments

Comments
 (0)