LeetCode------11. Container With Most Water

来源:互联网 发布:模拟核弹爆炸软件 编辑:程序博客网 时间:2024/06/08 01:11

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

Subscribe to see which companies asked this question

Runtime Error Message:Line 6: java.lang.ArrayIndexOutOfBoundsException: 2
Last executed input:[1,1]
Input:[1,1]
Output:0
Expected:1
public class Solution {    public int maxArea(int[] height) {        int left=0,right=height.length-1;        int area=0;        while(left<right){            area=Math.max(area,Math.min(height[left],height[right])*(right-left));            if(height[left]>height[right]){                right--;            }           // area=Math.max(area,Math.min(height[left],height[right])*(right-left));                    else{                left++;            }                    }        return area;    }}


0 0
原创粉丝点击