当前位置:网站首页>[leetcode] 11. Container with the most water
[leetcode] 11. Container with the most water
2022-06-25 01:34:00 【Xiaoqu】
11、 Container for the most water
subject :
Given a length of n Array of integers for height . Yes n Vertical line , The first i The two ends of the line are (i, 0) and (i, height[i]) .
Find two of them , Make them x A container of shafts can hold the most water .
Return the maximum amount of water that the container can store .
explain : You can't tilt the container .
Input :[1,8,6,2,5,4,8,3,7]
Output :49
explain : The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case , The container can hold water ( In blue ) The maximum value of is 49.
Example 2:
Input :height = [1,1]
Output :1
Tips :
n == height.length
2 <= n <= 10^5
0 <= height[i] <= 10^4
Their thinking :
Take a closer look at the following example 1 Graph , Discovery is to find the largest area of the area . The title also said a lot of nonsense .
In a word : Find the largest group (i,j), bring Area Maximum ;
Dynamic programming and double pointers are needed here , The basic expression is :Area = Max(min(height[i], height[j]) * (j-i))
{ among 0 <= i < j < height,size()};
- Using two Pointers , The pointer with a small value moves inward , This reduces the search space
- Because the area depends on the product of the distance of the pointer and the small value
- If the larger value moves inward , The distance must be reduced
- The other multiplier of the area must be less than or equal to the smaller value , Therefore, the area must be reduced
- And we require the largest area , Therefore, the pointer with large value does not move , The pointer with small value moves inward
Reference code :
class Solution {
public int maxArea(int[] a) {
int max = 0;
for(int i = 0, j = a.length - 1; i < j ; ){
int minHeight = a[i] < a[j] ? a[i ++] : a[j --];
max = Math.max(max, (j - i + 1) * minHeight);
}
return max;
}
}
边栏推荐
- 粉丝福利,JVM 手册(包含 PDF)
- ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
- Reverse ordinal number by merge sort
- 海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
- Redis basic commands and types
- How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
- Deoxyribonuclease I instructions in Chinese and English
- (CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
- Fan benefits, JVM manual (including PDF)
- 数组中关于sizeof()和strlen
猜你喜欢
How to store dataframe data in pandas into MySQL
Abnova BSG monoclonal antibody description in Chinese and English
Assembly language (4) function transfer parameters
粉丝福利,JVM 手册(包含 PDF)
天书夜读笔记——深入虚函数virtual
Bi SQL alias
明日考试 最后一天如何备考?二造考点攻略全整理
Ideas and examples of divide and conquer
最长连续序列[扩散法+空间换时间]
PMP考试“临门一脚”如何踢得漂亮?
随机推荐
Tencent moved!
1. 封装自己的脚手架 2.创建代码模块
高考之后,必然会出现以下四种情况:
为猪脸识别而进行自己数据集的构建、训练「建议收藏」
天书夜读笔记——深入虚函数virtual
结合实操带你吃透Redis持久化
天书夜读笔记——8.4 diskperf反汇编
Bi-sql create
动手学数据分析 数据建模和模型评估
Merge sort template & understanding
VB learning notes
Chinese and English instructions of Papain
IPC mechanism
String common methods
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
Bi-sql Union
excel 汉字转拼音「建议收藏」
全排列II[存在相同元素去重 + 标准回溯]
uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法
C语言边界计算和不对称边界