Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode 11. 盛最多水的容器 #78

Open
Chocolate1999 opened this issue Oct 8, 2020 · 1 comment
Open

LeetCode 11. 盛最多水的容器 #78

Chocolate1999 opened this issue Oct 8, 2020 · 1 comment
Labels
双指针 双指针经典题

Comments

@Chocolate1999
Copy link
Owner

仰望星空的人,不应该被嘲笑

题目描述

给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器,且 n 的值至少为 2。


图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例:

输入:[1,8,6,2,5,4,8,3,7]
输出:49

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/container-with-most-water
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

双指针做法,我们需要枚举所有情况,有一点贪心的思想,每次我们得看短的板子让我们容纳的面积。每次都选择左右指针最短的那个板子,计算出当前容纳的最多的水,然后从短的板子指针出发向内缩,这样不断求,最终我们可以枚举所有情况,自然可以枚举出最大容器面积。

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
    let len = height.length;
    let L = 0;
    let R = len - 1;
    let res = 0;
    while (L < R) {
        if (height[L] < height[R]) {  // 选择短板效应
            let ans = height[L] * (R - L);
            L++;
            res = Math.max(res, ans); // 求当前容纳最多的水
        } else {
            let ans = height[R] * (R - L);
            res = Math.max(res, ans);
            R--;
        }
    }
    return res;
};

最后

文章产出不易,还望各位小伙伴们支持一波!

往期精选:

小狮子前端の笔记仓库

leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)

小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!

访问超逸の博客,方便小伙伴阅读玩耍~

学如逆水行舟,不进则退
@Chocolate1999 Chocolate1999 added the 双指针 双指针经典题 label Oct 8, 2020
@Chocolate1999
Copy link
Owner Author

二刷

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
    let len = height.length;
    let L = 0, R = len - 1;
    let res = 0;
    while (L < R) {
        if (height[L] < height[R]) {
            res = Math.max(res, height[L] * (R - L));
            L++;
        } else {
            res = Math.max(res, height[R] * (R - L));
            R--;
        }
    }
    return res;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
双指针 双指针经典题
Projects
None yet
Development

No branches or pull requests

1 participant