题目链接:点击这里
首先我们不考虑高度的话 最大的面积应该是l r 应该是最边上的值 ,我们要取最大 所以 要维护从左到右单调增,从右到左 单调增 这样我们才能保证 面积增加
public static int maxArea(int[] height) { int ans = 0; int l = 0,r = height.length-1; while(l<r) { int h = Math.min(height[l],height[r]); ans = Math.max(h*(r-l), ans); if(height[l]<height[r]) { l++; }else { r--; } } return ans; }
Runtime: 2 ms, faster than 97.98% of Java online submissions for Container With Most Water.
Memory Usage: 40.7 MB, less than 15.30% of Java online submissions forContainer With Most Water