当前位置:网站首页>双指针进阶--leetcode题目--盛最多水的容器
双指针进阶--leetcode题目--盛最多水的容器
2022-04-23 17:22:00 【emo~~~~】
题目链接
11. 盛最多水的容器 - 力扣(LeetCode) (leetcode-cn.com)https://leetcode-cn.com/problems/container-with-most-water/这题我第一个想法是滑动窗口算法,因为我曾经深受其益
int maxArea(vector<int>& height) {
int len = height.size();
int maxx = 0;
vector<int>ret = height;
sort(ret.begin(), ret.end());
int s = ret[len - 1];
for (int i = len; i > 1; i--)
{
int left = 0;
int right = left + i - 1;
while (right <= len - 1)
{
int h = min(height[left], height[right]);
int l = i - 1; //长度是两线之间空格的个数!!!
maxx = max(maxx, h * l);
left++;
right++;
}
}
if (s >= maxx)
return s;
else
return maxx;
}
简单是简单,看一下时间复杂度,不出意外的超时了!!
在优化时,不经意想到了双指针,通过双指针的前后遍历可以很快的解决此题;
1、我们还是从长度最长开始,只是不需要每次都判断很多次
2、判断左指针和右指针的大小,每次只移动较小的指针,因为,如果移动大指针,后面的面积只会比现在的更小(很显然,由于长度从大到小,所以大指针只能向左移。如果左移了大指针,容器的长度减小了,但由于现在容器的真实长度(小指针)没有变,所以容器的高度不会增加,所以移动大指针容器的容积只减不增!)
3、比较选择容器容积的最大值,当遍历完成之后,即得到答案
其实,这题是一道经典的双指针算法题!
int maxArea(vector<int>& height) {
int len = height.size();
int left = 0;
int right = len - 1;
int maxx = 0;
while (left <= right)
{
int h = min(height[left], height[right]);
int l = right - left;
maxx = max(maxx,h * l);
if (height[left] < height[right])
{
left++;
}
else
{
right--;
}
}
return maxx;
}
int main()
{
vector<int>height;
for (int i = 0; i < 2; i++)
{
int a = 0;
cin >> a;
height.push_back(a);
}
int ans = maxArea(height);
cout << ans;
return 0;
}
希望和诸君共勉!!!
版权声明
本文为[emo~~~~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61567032/article/details/124360763
边栏推荐
- Shell script -- shell programming specification and variables
- 2.Electron之HelloWorld
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
- El cascade and El select click elsewhere to make the drop-down box disappear
- Some problems encountered in recent programming 2021 / 9 / 8
- Low code development platform sorting
- El date picker limits the selection range from the current time to two months ago
- [C#] 彻底搞明白深拷贝
- PC电脑使用无线网卡连接上手机热点,为什么不能上网
- Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
猜你喜欢
Detailed explanation of Milvus 2.0 quality assurance system
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Compare the performance of query based on the number of paging data that meet the query conditions
01-初识sketch-sketch优势
Use of todesk remote control software
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
Bottom processing of stack memory in browser
[PROJECT] small hat takeout (8)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
随机推荐
Baidu Map 3D rotation and tilt angle adjustment
Come out after a thousand calls
Advantages and disadvantages of several note taking software
C listens for WMI events
2. Electron's HelloWorld
Using quartz under. Net core - [1] quick start
Expression "func" tSource, object "to expression" func "tSource, object" []
1-3 nodejs installation list configuration and project environment
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
Shell - introduction, variables, and basic syntax
Milvus 2.0 détails du système d'assurance de la qualité
Compare the performance of query based on the number of paging data that meet the query conditions
Indexes and views in MySQL
. net cross platform principle (Part I)
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Calculation formula related to tolerance analysis
[related to zhengheyuan cutting tools]
Further study of data visualization
Promise (III)
Signalr can actively send data from the server to the client