11. Container With Most Water

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 and n is at least 2.

Solution1:

思路:Two pointers from two ends,兩端比較 小的移動,并每次compare and update max_area
Time Complexity: O(N) Space Complexity: O(1)

reference: kongweihan https://discuss.leetcode.com/topic/3462/yet-another-way-to-see-what-happens-in-the-o-n-algorithm

We start by computing the volume at (1,6), denoted by o. Now if the left line is shorter than the right line, then all the elements left to (1,6) on the first row have smaller volume, so we don't need to compute those cases (crossed by ---).

  1 2 3 4 5 6
1 x ------- o
2 x x
3 x x x 
4 x x x x
5 x x x x x
6 x x x x x x

Next we move the left line and compute (2,6). Now if the right line is shorter, all cases below (2,6) are eliminated.

  1 2 3 4 5 6
1 x ------- o
2 x x       o
3 x x x     |
4 x x x x   |
5 x x x x x |
6 x x x x x x

And no matter how this o path goes, we end up only need to find the max value on this path, which contains n-1 cases.

  1 2 3 4 5 6
1 x ------- o
2 x x - o o o
3 x x x o | |
4 x x x x | |
5 x x x x x |
6 x x x x x x

Solution2:Round1

Solution1 Code:

class Solution {
    public int maxArea(int[] height) {
        int max_area = 0;
        int start = 0, end = height.length - 1;
        while(start < end) {
            // update result
            int cur_max = Math.min(height[start], height[end]) * (end - start);
            if(cur_max > max_area)
                max_area = cur_max;
            
            // compare and move
            if(height[start] >= height[end]) {
                end--;
            }
            else {
                start++;
            }
        }
        return max_area;  
    }
}

Solution2 Round Code:

class Solution {
    public int maxArea(int[] height) {
        if(height == null || height.length == 0) return 0;
        int left = 0, right = height.length - 1;
        
        int max_area = Integer.MIN_VALUE;
        while(left < right) {
            int cur_area = Math.min(height[left], height[right]) * (right - left);
            max_area = Math.max(max_area, cur_area);
            if(height[left] <= height[right]) {
                left++;
            }
            else {
                right--;
            }
        }
        return max_area;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 插頭視角: 我是插頭,這一世的名字,我喜歡這個名字,喜歡老男生笑瞇瞇地這樣叫我,然后沖向他彎曲的手臂,坐上去。...
    陌上花續(xù)閱讀 751評論 0 0
  • 生物化學(xué),分析化學(xué)作業(yè)很多感到窒息,但驢上路了就拉不回來了,我是這樣形容我和小屆的關(guān)系,學(xué)業(yè)忙到?jīng)]頭沒腦,事情很多...
    易燃的小燃閱讀 287評論 0 0
  • 如果這是命,生活得有多糟! 每次都鼓不起勇氣,用某種方式來放棄自己!我不知道海子是花費了多少的決心,他臥上鐵軌看到...
    天下廬閱讀 180評論 0 0
  • 你會發(fā)現(xiàn)它們在你的附近 這些麻雀,前世的窮親戚都穿著灰褐色的 外套,它們小心翼翼的 尋找食物,尋找一個足以消除饑餓...
    桃都別園閱讀 100評論 0 0