LeetCode【11】 盛水最多的容器

题目:

分析:

1、双指针,储水为(R-L )* 二者较小高度,如题目,(9-2)* 7 = 49

2、双指针向中间靠,每次移动较矮的指针。

代码:

java 复制代码
public int maxArea(int[] height) {
     int left = 0;
     int right = height.length - 1;
     int max = 0;
     while (left < right) {
         if (height[right] > height[left]) {
             max = Math.max(max, (right - left) * height[left]);
             left++;
         } else {
             max = Math.max(max, (right - left) * height[right]);
             right--;
         }
     }

     return max;
 }
相关推荐
Jasmine_llq18 小时前
《P3572 [POI 2014] PTA-Little Bird》
算法·滑动窗口·单调队列·动态规划(dp)·多组查询处理·循环优化(宏定义 rep)
tankeven18 小时前
HJ101 排序
c++·算法
流云鹤18 小时前
动态规划02
算法·动态规划
青春易逝丶18 小时前
术语缩写
java
小白菜又菜18 小时前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
python·算法·leetcode
不想看见40418 小时前
01 Matrix 基本动态规划:二维--力扣101算法题解笔记
c++·算法·leetcode
ideal-cs18 小时前
总结:Nginx配置文件案例说明
java·运维·nginx·nginx配置文件
多恩Stone18 小时前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
踢足球092919 小时前
寒假打卡:2026-2-23
数据结构·算法
无尽的沉默19 小时前
Thymeleaf 基本语法和表达式
java·开发语言