<!--暴力解法,双重循环
时间复杂度是O(n ^ 2),空间复杂度是O(1)
-->
public class Solution {
public int maxArea(int[] height) {
int area = 0;
for (int i = 0; i < height.length - 1; i++) {
int tempArea = 0;
for (int j = i + 1; j < height.length; j++) {
tempArea = Math.max(tempArea, (j - i) * Math.min(height[i], height[j]));
}
area = Math.max(area, tempArea);
}
return area;
}
}
<!--双指针
时间复杂度为O(n),空间复杂度是O(1)
-->
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(ans, area);
if (height[l] <= height[r]) {
++l;
}
else {
--r;
}
}
return ans;
}
}