-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-MaxArea.py
More file actions
60 lines (52 loc) · 1.5 KB
/
11-MaxArea.py
File metadata and controls
60 lines (52 loc) · 1.5 KB
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
# Python3
class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height) - 1
ans = 0
while l < r:
area = min(height[l], height[r]) * (r - l)
ans = max(ans, area)
if height[l] <= height[r]:
l += 1
else:
r -= 1
return ans
# 双指针 时间复杂度O(N) 空间复杂度O(1)
# 双指针代表的是可以作为容器边界的所有位置的范围。在一开始,双指针指向数组的左右边界,表示数组
# 中所有的位置都可以作为容器的边界。之后每次将对应的数字较小的指针往另一个指针的方向移动一个位置。
# C++
class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size() - 1;
int ans = 0;
while (l < r) {
int area = min(height[l], height[r]) * (r - l);
ans = max(ans, area);
if (height[l] <= height[r]) {
l++;
} else {
r--;
}
}
return ans;
}
};
# Java
public class Solution {
public int maxArea(int[] height) {
int l = 0, r = height.length - 1;
int ans = 0;
while (l < r) {
int area = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(area, ans);
if (height[l] < height[r]) {
l++;
} else {
r--;
}
}
return ans;
}
}