leetcode-11. 盛最多水的容器(双指针)

11. 盛最多水的容器
js 复制代码
/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
    // 时间复杂度 O(n)
    // 空间复杂度 O(1)
  let len = height.length;
  let left = 0,
    right = len - 1;
  let res = 0;

  while (left < right) {
    let area = Math.min(height[left], height[right]) * (right - left);
     res = Math.max(res, area);
    if (height[left] < height[right]) {
      left++;
    } else {
      right--;
    }
  }
  return res;
};

console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));
py 复制代码
class Solution:
    def maxArea(self,height:List[int])->int:
        ans = 0
        left = 0
        right = len(height)-1
        while(left<right):
            area = (right-left)*min(height[left],height[right])
            ans = max(area,ans)
            if(height[left]<height[right]):
                left+=1
            else:
                right -=1
        return ans
reference

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

相关推荐
Evand J3 小时前
【课题推荐】强跟踪UKF算法,三维非线性状态量和观测量,附MATLAB代码测试结果
开发语言·算法·matlab
爱炸薯条的小朋友3 小时前
全局锁的性能优势,以及链路优化为何常常低于预期——基于 `MatPoolsTest` 中小图池与大图池的实战复盘
opencv·算法·c#
NCU_wander3 小时前
全品类存储芯片汇总/DRAM/flash/HBM
算法
Plan-C-4 小时前
二叉树的遍历
java·数据结构·算法
靠沿4 小时前
【动态规划算法】专题二——路径问题
算法·动态规划
手写码匠4 小时前
手写 AI 推理加速引擎:从零实现 KV Cache 与 Speculative Decoding
人工智能·深度学习·算法·aigc
无限进步_4 小时前
【C++】可变参数模板与emplace系列
java·c++·算法
一切皆是因缘际会4 小时前
AI Agent落地困局与突破:从技术架构到企业解析
数据结构·人工智能·算法·架构
sheeta19984 小时前
LeetCode 每日一题笔记 日期:2026.05.16 题目:154. 寻找旋转排序数组中的最小值 II
笔记·算法·leetcode