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/

相关推荐
光影少年5 分钟前
AI Agent、自动化与未来工作
人工智能·算法·架构
牧羊人.33316 分钟前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
考研保研资料分享23 分钟前
双非新闻学保研复盘:东华小组展示、长安面试与兰大预推免
面试·职场和发展
Nil20828 分钟前
leetcode 72编辑距离
算法·leetcode·职场和发展
神工坊1 小时前
技术分享︱SIMFORGE HSF® 网格加密与热点加速技术实践
算法·hpc·网格·cae·工程仿真·数值引擎·samr
Nil2081 小时前
leetcode 136只出现一次的数字
leetcode·哈希算法·散列表
CV@CV1 小时前
世界模型与智能体协同推理逻辑
人工智能·算法·边缘计算·量子计算·具身智能
jiayoushijie-泽宣1 小时前
SHIFT-PathFormer:专家轨迹蒸馏、连续安全优化与超大迷宫实验
算法
猿究院_夏燊2 小时前
SpringMVC 到底是怎么干活的?
java·网络·职场和发展
UIU1142 小时前
P1003 [NOIP 2011 提高组] 铺地毯
数据结构·c++·算法