首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >11. 盛最多水的容器

11. 盛最多水的容器

作者头像
lucifer210
发布2019-09-16 16:41:28
发布2019-09-16 16:41:28
36500
举报
文章被收录于专栏:脑洞前端脑洞前端
运行总次数:0

题目描述

代码语言:javascript
代码运行次数:0
运行
复制
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.
代码语言:javascript
代码运行次数:0
运行
复制
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

思路

符合直觉的解法是,我们可以对两两进行求解,计算可以承载的水量。然后不断更新最大值,最后返回最大值即可。这种解法,需要两层循环,时间复杂度是O(n^2)

eg:

代码语言:javascript
代码运行次数:0
运行
复制
// 这个解法比较暴力,效率比较低
    // 时间复杂度是O(n^2)
    let max = 0;
    for(let i = 0; i < height.length; i++) {
        for(let j = i + 1; j < height.length; j++) {
            const currentArea = Math.abs(i - j) * Math.min(height[i], height[j]);
            if (currentArea > max) {
                max = currentArea;
            }
        }
    }
    return max;

这种符合直觉的解法有点像冒泡排序, 大家可以稍微类比一下

那么有没有更加优的解法呢?我们来换个角度来思考这个问题,上述的解法是通过两两组合,这无疑是完备的, 那我门是否可以先计算长度为n的面积,然后计算长度为n-1的面积,... 计算长度为1的面积。这样去不断更新最大值呢?很显然这种解法也是完备的,但是似乎时间复杂度还是O(n ^ 2), 不要着急。

考虑一下,如果我们计算n-1长度的面积的时候,是直接直接排除一半的结果的。

如图:

比如我们计算n面积的时候,假如左侧的线段高度比右侧的高度低,那么我们通过左移右指针来将长度缩短为n-1的做法是没有意义的, 因为 新的形成的面积变成了(n-1)*heightOfLeft这个面积一定比刚才的长度为n的面积nn*heightOfLeft小

也就是说最大面积 一定是当前的面积或者通过移动短的线段得到

关键点解析

  • 双指针优化时间复杂度

代码

  • 语言支持:JS,C++

JavaScript Code:

代码语言:javascript
代码运行次数:0
运行
复制
/*
 * @lc app=leetcode id=11 lang=javascript
 *
 * [11] Container With Most Water
 *
 * https://leetcode.com/problems/container-with-most-water/description/
 *
 * algorithms
 * Medium (42.86%)
 * Total Accepted:    344.3K
 * Total Submissions: 790.1K
 * Testcase Example:  '[1,8,6,2,5,4,8,3,7]'
 *
 * 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
 *
 */
/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    if (!height || height.length <= 1) return 0;

    // 双指针来进行优化
    // 时间复杂度是O(n)
    let leftPos = 0;
    let rightPos = height.length - 1;
    let max = 0;
    while(leftPos < rightPos) {

        const currentArea = Math.abs(leftPos - rightPos) * Math.min(height[leftPos] , height[rightPos]);
        if (currentArea > max) {
            max = currentArea;
        }
        // 更新小的
        if (height[leftPos] < height[rightPos]) {
            leftPos++;
        } else { // 如果相等就随便了
            rightPos--;
        }
    }

    return max;
};

C++ Code:

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxArea(vector<int>& height) {
        auto ret = 0ul, leftPos = 0ul, rightPos = height.size() - 1;
        while( leftPos < rightPos)
        {
            ret = std::max(ret, std::min(height[leftPos], height[rightPos]) * (rightPos - leftPos));
            if (height[leftPos] < height[rightPos]) ++leftPos;
            else --rightPos;
        }
        return ret;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 脑洞前端 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 思路
  • 关键点解析
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档