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

边栏推荐
- sql 聚合函数对 null 的处理[通俗易懂]
- 动手学数据分析 数据建模和模型评估
- 纹理增强
- 百度语音合成语音文件并在网站中展示
- Hands on data analysis data modeling and model evaluation
- lnmp环境安装ffmpeg,并在Yii2中使用
- How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
- 腾讯搬家了!
- 腾讯完成全面上云 打造国内最大云原生实践
- AssertionError: CUDA unavailable, invalid device 0 requested
猜你喜欢

JVM directive

1. package your own scaffold 2 Create code module

Fan benefits, JVM manual (including PDF)

js数组对象转对象

Tianshu night reading notes -- memory paging mechanism

15. several methods of thread synchronization

Abnova 5-methylcytosine polyclonal antibody

Bi-sql like

Linux64Bit下安装MySQL5.6-不能修改root密码
The latest QQ wechat domain name anti red PHP program source code + forced jump to open
随机推荐
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
Baidu voice synthesizes voice files and displays them on the website
【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
1. 封装自己的脚手架 2.创建代码模块
Huawei laptop, which grew against the trend in Q1, is leading PC into the era of "smart office"
多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
纹理增强
Combined with practice, you will understand redis persistence
百度语音合成语音文件并在网站中展示
Bi-sql like
Poj3669 meteor shower (BFS pretreatment)
Bi SQL constraints
Reverse ordinal number by merge sort
RedisTemplate操作Redis,这一篇文章就够了(一)[通俗易懂]
Program.launch(xxx)打开文件
天书夜读笔记——内存分页机制
Bi-sql select into
Texture enhancement
sql 聚合函数对 null 的处理[通俗易懂]
Bi SQL drop & alter