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.

在这里插入图片描述

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

注:这道题好评率挺高,因为下午温习了下 python 基础所以开始的时候用 python
写的。知道 python 慢,但是不知道是这么的慢。因为时间复杂度是O(n^2),所以直接超时了。只能再改成Java的,时间也达到了350ms。

1.Java 版

class Solution {
    public int maxArea(int[] height) {
        int length = height.length;
        int h,l;
        int result = 0;
        if(length < 2)
            return 0;
        for(int i = 0; i < length-1; ++i)
            for(int j = i+1; j < length; ++j){
                if(height[i] > height[j])
                    h = height[j];
                else
                    h = height[i];
                l = h*(j-i);
                if(result < l)
                    result = l;
            }
        return result;
    }
}

2.python版

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        if len(height) < 2:
            return 0
        result = 0
        for i in range(len(height) - 1):
            for j in range(i + 1,len(height)):
                if height[i] >= height[j]:
                    h = height[j]
                else:
                    h = height[i]
                l = j - i
                if h*l > result:
                    result = h * l
        return result

3.Java版(更新后)
昨天晚上提交上后,因为运行时间太长一直在想如何改进算法。然后突然想到可不可以设置两个指针从两头向中间移动,然后根据一个条件来决定移左边的还有右边的。运行效果还是特别显著的。
在这里插入图片描述
从 350 ms 一下缩短到了 9 ms,不过仍然只打败了百分之四十多的人,还要再接再厉。

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

** 4. 最终版 **
我感觉算法的时间复杂度已经最优了啊!那可能就是使用的语言和代码编写的问题了。我开始进行了一一的尝试,用C++然后用C。
在这里插入图片描述

在改成cpp 后用时变长了,但是打败的人也变多了。改成C后,时间果然直接变短了,但是排名仍然不好。我发现原来代码中两行代码可以合成一个,然后尝试着改了一下,用时直接缩短了一半。

int maxArea(int* height, int heightSize) {
    int low = 0, high = heightSize - 1;
        int max = 0,temp,h;
        while(low < high){
            h = height[low] < height[high] ? height[low] : height[high];
            /*****注释代码为修改前*****/
            //temp = h * (high - low);
            //max = temp > max ? temp : max;
            /*****修改后*****/
            if(max < h * (high - low))
                max = h * (high - low);
            /***************/
            if(height[low] < height[high])
                ++low;
            else
                --high;
        }
        return max;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值