[LeetCode]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.
最大面积
       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.
       题目大意:给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

样例

Example:

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

python解法

暴力解法

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        for index, val in enumerate(height, start=1):
            for i, v in enumerate(height[index:], index+1):
                value = min(val, v)*(i-index)
                max_area = value if value>max_area else max_area
        return max_area

题后反思:

  1. 暴力解法时间复杂度为 O ( n 2 ) O(n^2) O(n2), 空间复杂度为 O ( 1 ) O(1) O(1)
  2. 暴力解法无法解决LeetCode这道题。需要另外想办法减少时间复杂度。

【另解】

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        i = 0
        i_end = len(height) -1
        while i < i_end:
        	if height[i] < height[i_end]:
                max_area = max(max_area,(i_end - i)*height[i])
                i += 1
            else:
                max_area = max(max_area,(i_end - i)*height[i_end])
                i_end -= 1
        return max_area

Runtime: 140 ms,
Memory Usage: 15.6 MB,
题后反思:

  1. 采用了两头向中间寻找最大盛水量,时间复杂度大大减少,变为 O ( n ) O(n) O(n)

       算法可以再优化, 当向中间移动时,小于两个数据的较小值h时,不需要计算,直接移动下标即可。

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        i = 0
        i_end = len(height) -1
        while i < i_end:
			h = min(height[i],height[i_end])
            max_area = max(max_area,(i_end - i)*h)
            while height[i]<=h and i<i_end:
                i += 1
            while height[i_end]<=h and i<i_end:
                i_end -= 1
        return max_area

C语言解法

int maxArea(int* height, int heightSize){
    int left = 0, right = heightSize-1, h = 0;
    int max_area = 0;
    while(left < right)
    {
        h = height[left]<height[right]?height[left]:height[right];
        if(max_area < (right-left)*h)
            max_area = (right-left)*h;
        while(height[left]<=h && left < right)
            left ++;
        while(height[right]<=h && left < right)
            right --;
    }
    return max_area;
}

Runtime: 8 ms,
Memory Usage: 7.9 MB,
题后反思:

  1. c语言是真的快呀!!!

文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值