当前位置:网站首页>【LeetCode】11. Container with the most water
【LeetCode】11. Container with the most water
2022-06-11 21:58:00 【When the flower does not wither】

The easiest way to think of is to set two pointers to traverse all the cases in turn , Then find the biggest one , But the time complexity of this method is O(n2), The result of the last run will time out , So a better way is needed .
Ideas : Two pointers at the beginning, one to the beginning and one to the end , At this time, the bottom of the container is the largest , Next, as the pointer moves inward , It will make the bottom of the container smaller , In this case, you want to make the container contain more water , It's just about the height of the container . So how do we decide which pointer to move ? We can find that whether the left pointer moves one bit to the right , Or the right pointer moves one bit to the left , The bottom of the container is the same , Are less than before 1. In this case, we want to increase the area of the container after the pointer is moved , Make the height of the moved container as large as possible , So we choose the pointer with the lower height to move , So we keep the higher side of the container , Give up the smaller side , To get a chance to have a higher edge .
class Solution {
public:
int maxArea(vector<int>& height) {
int maximum = 0;
int low = 0, high = height.size() - 1;
while(low < high){
int h = min(height[low], height[high]);
maximum = max(maximum, h * (high - low));
if(height[low] < height[high]) low++; // The one with small moving height
else high--;
}
return maximum;
}
};
边栏推荐
- C语言实现八种排序(3)
- R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model
- 每日一题-删除有序数组的重复项
- Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
- 类和对象(2)
- 揭秘爆款的小程序,为何一黑到底
- R语言书籍学习03 《深入浅出R语言数据分析》-第七章 线性回归模型
- Look for leap years and see how many leap years I have had since I was born (I have had five)
- 动态内存管理(1)
- 238. product of arrays other than itself
猜你喜欢

Conception du Processeur superscalaire Yao yongbin chapitre 2 cache - - sous - section 2.4 extrait

All features of polymorphism

Classes and objects (2)
![[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?](/img/cd/e7ffecbee13596f2298ee8c0a5b873.jpg)
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

Carry and walk with you. Have you ever seen a "palm sized" weather station?
![[niuke.com] dp31 [template] complete Backpack](/img/81/5f35a58c48f05a5b4b6bdc106f5da0.jpg)
[niuke.com] dp31 [template] complete Backpack

Classes and objects (3)

学习位段(1)

如何查看win系统的安装日期
随机推荐
Huawei equipment configuration h-vpn
C language implements eight sorts (1)
「大模型」之所短,「知识图谱」之所长
zypper命令使用示例
Latex combat notes 3- macro package and control commands
自定义实现offsetof
RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
On the night of the joint commissioning, I beat up my colleagues
每日一题 -- 验证回文串
如何查看win系统的安装日期
实现栈和队列
Conception du Processeur superscalaire Yao yongbin chapitre 2 cache - - sous - section 2.4 extrait
每日一题 - 罗马数字转整数
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?
win11怎么看电脑显卡信息
RPA+低代码助推品牌电商启新创变、重启增长
相对完善的单例模式
Leetcode-155-minimum stack
动态内存管理(1)
Leetcode-129- sum of numbers from root node to leaf node