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/

相关推荐
信奥卷王8 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵8 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG10 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z10 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛11 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记11 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序11 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室11 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
圣保罗的大教堂12 小时前
leetcode 301. 删除无效的括号 困难
leetcode