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

盛最多水的容器 - Rust

作者: 曾大稳丶 | 来源:发表于2022-07-22 11:05 被阅读0次
image.png
image.png

题目解析
采用双指针,从头和尾分别移动,每一次移动 height[i],height[j] 小的那一个

/// https://leetcode.cn/problems/container-with-most-water/
pub fn max_area(height: Vec<i32>) -> i32 {
    //  采用双指针,从头和尾分别移动,每一次移动 height[i],height[j] 小的那一个
    let (mut max, mut left,mut right) = (0,0,height.len()-1);
    while left<right {
        max = max.max((right-left)as i32 * height[left].min(height[right]));
        if height[left] < height[right]{
            left += 1;
        }else {
            right -= 1;
        }
    }
    max
}

复杂度分析
空间复杂度: O(1)。
时间复杂度: O(n)。

相关文章

  • 盛最多水的容器 - Rust

    题目解析采用双指针,从头和尾分别移动,每一次移动 height[i],height[j] 小的那一个 复杂度分析空...

  • 盛最多水的容器

    给定 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!), 当数量越大计算时间越长,...

网友评论

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

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