当前位置:网站首页>[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;
}
}

边栏推荐
- 论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
- Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
- TC对象结构和简称
- 天书夜读笔记——8.4 diskperf反汇编
- js数组对象转对象
- 全排列II[存在相同元素去重 + 标准回溯]
- Abnova a4gnt polyclonal antibody
- Deep learning LSTM model for stock analysis and prediction
- PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
- 谷歌浏览器控制台 f12怎么设置成中文/英文 切换方法,一定要看到最后!!!
猜你喜欢

Abnova 5-methylcytosine polyclonal antibody

Abnova丨A4GNT多克隆抗体中英文说明

Hands on data analysis data modeling and model evaluation

Deep learning LSTM model for stock analysis and prediction

pbcms添加循环数字标签

Cobalt strike installation tutorial

Bi skill - judge 0 and null

天书夜读笔记——深入虚函数virtual

Using macro code to generate handwriting automatically in word or WPS

Abnova丨5-甲基胞嘧啶多克隆抗体中英文说明
随机推荐
腾讯云WeCity解决方案
Abnova丨BSG 单克隆抗体中英文说明
Introduction to bi-sql wildcards
WinXP内核驱动调试
(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
C language boundary calculation and asymmetric boundary
1. 封装自己的脚手架 2.创建代码模块
IPC mechanism
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
Powerbi - for you who are learning
(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
最长连续序列[扩散法+空间换时间]
Reverse ordinal number by merge sort
Tencent cloud wecity Hello 2022!
RedisTemplate操作Redis,这一篇文章就够了(一)[通俗易懂]
CCNP的BGP部分笔记
期望与方差
After the college entrance examination, the following four situations will inevitably occur:
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
修身励学篇