java leetode Container With Most Water

来源:互联网 发布:淘宝店铺发布二手产品 编辑:程序博客网 时间:2024/06/05 22:14

left = 0;

right = length-1;

if we want to get a bigger area. the only way we can do is move the smaller side forward to another side.

the code is bellow.

public class Solution {    public int maxArea(int[] height) {    int left = 0;    int right = height.length -1;    int max = Math.min(height[left],height[right])*(height.length - 1);    while(left < right)    {        int temp = 0;        if(height[left] <= height [right])            left++;        else             right--;        temp = Math.min(height[left],height[right])*(right - left);        if(temp > max)            max = temp;    }    return max;    }}

0 0
原创粉丝点击