当前位置:网站首页>数组——11. 盛最多水的容器
数组——11. 盛最多水的容器
2022-07-23 19:17:00 【向着百万年薪努力的小赵】
1 题目描述
- 盛最多水的容器
给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
返回容器可以储存的最大水量。
说明:你不能倾斜容器。
2 题目示例

输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
示例 2:
输入:height = [1,1]
输出:1
3 题目提示
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
4 思路
矩阵的面积与两个因素有关:
矩阵的长度:两条垂直线的距离
矩阵的宽度:两条垂直线其中较短一条的长度
因此,要矩阵面积最大化,两条垂直线的距离越远越好,两条垂直线的最短长度也要越长越好。
我们设置两个指针 left 和 right,分别指向数组的最左端和最右端。此时,两条垂直线的距离是最远的,若要下一个矩阵面积比当前面积来得大,必须要把 height[left] 和 height[right] 中较短的垂直线往中间移动,看看是否可以找到更长的垂直线。
对于这种问题,不要想整体,而应该去想局部;本质就是动态规划思路,考虑如何处理没一个子问题即:位置 i,能装下多少水。
5 我的答案
/** * 双指针解法 * 时间复杂度降 O(N),空间复杂度 O(1) */
public int maxArea(int[] height) {
int size = height.length;
int result = 0;
int left_max = 0, right_max = 0;
int left = 0, right = size - 1;
while (left < right) {
left_max = Math.max(left_max, height[left]);
right_max = Math.max(right_max, height[right]);
int currentArea = 0;
//用 left 和 right 两个指针从两端向中心收缩,一边收缩一边计算 [left, right] 之间的矩形面积,取最大的面积值即是答案
//矩形的高度是由 min(height[left], height[right]) 即较低的一边决定的:
//移动较低的那一边,那条边可能会变高,使得矩形的高度变大,进而就「有可能」使得矩形的面积变大.
if (left_max < right_max) {
currentArea = left_max * (right - left);
left++;
} else {
currentArea = right_max * (right - left);
right--;
}
result = Math.max(result, currentArea);
}
return result;
}
边栏推荐
猜你喜欢

能量原理與變分法筆記19:最小餘能原理+可能功原理

MySQL data recovery - using the data directory

scanf()和getchar()的用法讨论

How to solve the problem that the solid state disk cannot be found when installing win11?
![[development experience] development project trample pit collection [continuous update]](/img/02/7bea3bf09e9a27b6ab74399639f197.png)
[development experience] development project trample pit collection [continuous update]

编译器LLVM-MLIR-Intrinics-llvm backend-instruction

How to reset the computer system? The method is actually very simple

梅科爾工作室-小熊派開發筆記2

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 6

Leetcode 228. summary interval (yes, solved)
随机推荐
Uncover the working principle of solid state disk
使用多态时,判断能否向下转型的两种思路
搭建自己的目标检测环境,模型配置,数据配置 MMdetection
Huawei cloud stack [interview]
2022 Shandong old age Expo, Shandong elderly care exhibition, China International elderly care service industry exhibition was held in September
Leetcode - the nearest sum of three numbers
Typescript新数据类型Symbol的使用
【无标题】
17.生命周期
scanf()和getchar()的用法讨论
QT With OpenGL(帧缓存篇)
能量原理与变分法笔记19:最小余能原理+可能功原理
QT with OpenGL (frame cache)
Set asp Net MVC site default page is the specified page
Energy principle and variational method note 14: summary + problem solving
Leetcode 219. 存在重复元素 II(可以,已解决)
What if there is no word document in win11? There is no word document solution tutorial in win11
C language leak detection and filling (1)
JDK installation package and MySQL installation package sorting
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC