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;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 插头视角: 我是插头,这一世的名字,我喜欢这个名字,喜欢老男生笑眯眯地这样叫我,然后冲向他弯曲的手臂,坐上去。...
    陌上花续阅读 4,026评论 0 0
  • 生物化学,分析化学作业很多感到窒息,但驴上路了就拉不回来了,我是这样形容我和小届的关系,学业忙到没头没脑,事情很多...
    易燃的小燃阅读 1,904评论 0 0
  • 如果这是命,生活得有多糟! 每次都鼓不起勇气,用某种方式来放弃自己!我不知道海子是花费了多少的决心,他卧上铁轨看到...
    天下庐阅读 1,222评论 0 0
  • 你会发现它们在你的附近 这些麻雀,前世的穷亲戚都穿着灰褐色的 外套,它们小心翼翼的 寻找食物,寻找一个足以消除饥饿...
    桃都别园阅读 695评论 0 0