Container with most water

題目比較簡單,每次找較小的板往裏移,得到的容器的容積最大的就是所求的容積

public class Solution {
    public static int maxArea(int[] height) {
        int begin = 0, high, end = height.length - 1;
        int max = 0;
        while (end >= begin) {
            high = height[begin] < height[end] ? height[begin] : height[end];
            max = high * (end - begin) > max ? high * (end - begin) : max;
            if (height[begin] < height[end])
                begin++;
            else
                end--;
        }
        return max;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章