zoukankan      html  css  js  c++  java
  • 11. Container With Most Water

    题目:

    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.

    链接:http://leetcode.com/problems/container-with-most-water/

    题解:Two Pointers, 每次移动至较小的一个pointer。Time Complexity - O(n), Space Complexity - O(1)

    public class Solution {
        public int maxArea(List<Integer> height) {
            if(height == null || height.size() < 2)
                return 0;
            int max = 0, left = 0, right = height.size() - 1;
            
            while(left < right){
                int curArea = Math.min(height.get(left), height.get(right)) * (right - left);
                max = Math.max(max, curArea);
                if(height.get(left) < height.get(right))
                    left++;
                else
                    right--;
            }
            
            return max;
        }
    }

    二刷:

    经典的Two Pointers方法,前后夹逼一下就可以得到结果了。二刷时发现signature变了。

    Java:

    public class Solution {
        public int maxArea(int[] height) {
            if (height == null || height.length == 0) {
                return 0;
            }
            int maxVolumn = 0;
            int lo = 0, hi = height.length - 1;
            while (lo < hi) {
                int curVolumn = Math.min(height[lo], height[hi]) * (hi - lo);
                maxVolumn = Math.max(maxVolumn, curVolumn);
                if (height[lo] < height[hi]) {
                    lo++;
                } else {
                    hi--;
                }
            }
            return maxVolumn;
        }
    }

    Python:

    class Solution(object):
        def maxArea(self, height):
            """
            :type height: List[int]
            :rtype: int
            """
            lo = 0
            hi = len(height) - 1
            maxVolumn = 0
            while lo < hi:
                curVolumn = min(height[lo], height[hi]) * (hi - lo)
                maxVolumn = max(maxVolumn, curVolumn)
                if height[lo] < height[hi]:
                    lo += 1
                else:
                    hi -= 1
            return maxVolumn

    题外话:

    Python真的短,希望深入学习以后能更短。

    三刷:

    这种小的code snippet一定要熟练,说不定可以用到哪一个大段的程序里。 像这道题的方法,就可以在"Trapping Rain Water"里面使用。很多算法,思想都是想通的,要多学多练,融会贯通。

    这里我们的面积等于左边和右边较小的bar * (hi - lo),每次都要尝试更新一下max。 之后,移动两根bar中较短的一根。

    注意这里的高度是两根bar中较小的一根,长度是 hi - lo。可以想象成两根细线中间夹的区域,并不是hi - lo + 1。

    Java:

    Time Complexity - O(n), Space Complexity - O(1)

    public class Solution {
        public int maxArea(int[] height) {
            if (height == null || height.length < 2) return 0;
            int lo = 0, hi = height.length - 1;
            int max = 0;
            while (lo < hi) {
                max = Math.max(Math.min(height[lo], height[hi]) * (hi - lo), max);
                if (height[lo] < height[hi]) lo++;
                else hi--;
            }
            return max;
        }
    }

    Reference:

    https://leetcode.com/discuss/1074/anyone-who-has-a-o-n-algorithm

  • 相关阅读:
    GUI学习笔记之一“Hello world”程序
    GDI和GUI的区别
    Convert.Int32、(int)和int.Parse三者的区别
    华为机试题汇总
    算法导论 第7章 课后习题
    算法导论 第8章 线性时间排序 课后习题
    算法导论 第21章 不相交集合的数据结构
    [转载]NIM(1) 一排石头的游戏
    算法导论 第22章 图论之拓扑排序
    编程珠玑第八章 算法设计艺术
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4430737.html
Copyright © 2011-2022 走看看