Problem Description
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
[https://leetcode.com/problems/container-with-most-water/]
思路
从两边开始作为杯壁计算容量,然后将矮的一边向中心移动一个位置。
一遍AC~~~~噢噢噢噢噢噢噢噢哦哦哦~
Code
package Q11;
public class Solution {
public static int maxArea(int[] height) {
int ans=0;
if(height.length<2) return ans;
int right=height.length-1;
int left=0;
int tmpArea;
while(left<right){
tmpArea=(right-left)*Math.min(height[left], height[right]);
ans=Math.max(ans, tmpArea);
if(height[left]<height[right]){
left++;
}
else{
right--;
}
}
return ans;
}
//
// public static void main(String[] args) {
// int[] a={2,4,3};
// System.out.print(maxArea(a));
// }
}