题目描述
这算是一道比较经典的动态规划题目了
优化之路…
求解
版一
class Solution {
public int maxArea(int[] height) {
int maxArea = 0;
for (int left = 0, right = height.length - 1; left < right; ) {
int lowerIndex = height[left] < height[right] ? left++ : right--;
maxArea = Math.max((right - left + 1) * height[lowerIndex], maxArea);
}
return maxArea;
}
}
版二
class Solution {
public int maxArea(int[] height) {
int maxArea = 0;
int left = 0, right = height.length - 1;
while (left < right) {
int lowHeight = height[left] < height[right] ? height[left++] : height[right--];
maxArea = Math.max((right - left + 1) * lowHeight, maxArea);
while (height[left] < lowHeight && left < right) left++;
while (height[right] < lowHeight && left < right) right--;
}
return maxArea;
}
}
版三
class Solution {
public int maxArea(int[] height) {
int maxArea = 0;
for (int left = 0, right = height.length - 1,lowHeight = 0; left < right;) {
lowHeight = height[left] < height[right] ? height[left] : height[right];
maxArea = Math.max((right - left ) * lowHeight, maxArea);
while (height[left] <= lowHeight && left < right) left++;
while (height[right] <= lowHeight && left < right) right--;
}
return maxArea;
}
}