美文网首页
盛最多水的容器

盛最多水的容器

作者: 7赢月 | 来源:发表于2020-04-20 09:52 被阅读0次

题目

https://leetcode-cn.com/problems/container-with-most-water/

func min(hi, hj int) int {
    if hi > hj {
        return hj
    }

    return hi
}

func maxArea(height []int) int {
    max := 0
    i, j := 0, len(height)-1

    for i != j {
        cur := (j - i) * min(height[i], height[j])
        if cur > max {
            max = cur
        }

        if height[i] > height[j] {
            j--
        } else {
            i++
        }
    }

    return max
}


思路

这道题很简单,一开始做的是暴力解发,后面看题解后发现使用两个指针能加快速度。
注意边界条件的判断

相关文章

  • 盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    盛最多水的容器 给定n个非负整数a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。画n条垂直线,使...

  • 盛最多水的容器

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/cont...

  • 盛最多水的容器

    题目描述 思路 题解代码

  • 盛最多水的容器

    盛最多水的容器 难度中等1826 收藏 分享 切换为英文 关注 反馈 给你 n 个非负整数 a1,a2,...,a...

  • 盛最多水的容器

    题目描述:  给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内...

  • 盛最多水的容器

    给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    题目 https://leetcode-cn.com/problems/container-with-most-w...

  • 盛最多水的容器

    题目: 题目的理解: 计算两个数之间的面积,获取到最大的面积。 时间复杂度为O(n!), 当数量越大计算时间越长,...

  • 盛最多水的容器

    LeetCode第十一题 题目描述:给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i...

网友评论

      本文标题:盛最多水的容器

      本文链接:https://www.haomeiwen.com/subject/nlulihtx.html