当前位置:网站首页>Double pointer advanced -- leetcode title -- container with the most water
Double pointer advanced -- leetcode title -- container with the most water
2022-04-23 17:29:00 【emo~~~~】
Topic link
11. Container for the most water - Power button (LeetCode) (leetcode-cn.com)
https://leetcode-cn.com/problems/container-with-most-water/
My first thought on this question is Sliding window algorithm , Because I used to benefit from it
Three in a row --- Sliding window algorithm ---leetcode_emo~~~~ The blog of -CSDN Blog
https://blog.csdn.net/qq_61567032/article/details/123878549?spm=1001.2014.3001.5501 Just like this. : Everyone is simple
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; // The length is the number of spaces between two lines !!!
maxx = max(maxx, h * l);
left++;
right++;
}
}
if (s >= maxx)
return s;
else
return maxx;
}
Simple is simple , Look at the time complexity , No unexpected overtime !!
In optimization , Inadvertently thought of the double pointer , This problem can be solved quickly through the forward and backward traversal of double pointers ;
1、 Let's start with the longest , Just don't need to judge many times every time
2、 Determine the size of the left and right pointers , Move only the smaller pointer at a time , because , If you move the big pointer , The back area will only be smaller than now ( Obviously , Due to the length from large to small , So the big pointer can only move to the left . If you move the big pointer to the left , The length of the container is reduced , But now the real length of the container ( Little pointer ) No change , So the height of the container will not increase , Therefore, the volume of the moving large pointer container only decreases but does not increase !)
3、 Compare and select the maximum container volume , When the traversal is complete , That's the answer
Actually , This problem is a classic double pointer algorithm problem !
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;
}
I hope to share with you !!!
版权声明
本文为[emo~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231722291467.html
边栏推荐
- Manually implement call, apply and bind functions
- ClickHouse-SQL 操作
- Wiper component encapsulation
- uni-app黑马优购项目学习记录(下)
- SiteServer CMS5. 0 Usage Summary
- Seven cattle upload pictures (foreground JS + background C API get token)
- Your brain expands and shrinks over time — these charts show how
- node中,如何手动实现触发垃圾回收机制
- Entity Framework core captures database changes
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
猜你喜欢

C# Task. Delay and thread The difference between sleep

Go language, array, string, slice

2. Electron's HelloWorld

ASP. Net core JWT certification

1-1 NodeJS

If you start from zero according to the frame

PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet

Signalr can actively send data from the server to the client

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

ASP. NET CORE3. 1. Solution to login failure after identity registers users
随机推荐
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
If you start from zero according to the frame
Baidu Map Case - Zoom component, map scale component
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Advantages and disadvantages of several note taking software
Solution of Navicat connecting Oracle library is not loaded
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Document operation II (5000 word summary)
为什么有些人说单片机简单,我学起来这么吃力?
Shell-入门、变量、以及基本的语法
groutine
For the space occupation of the software, please refer to the installation directory
Understanding and small examples of unity3d object pool
Conversion between hexadecimal numbers
Handwritten event publish subscribe framework
Node template engine (EJS, art template)
Model problems of stock in and stock out and inventory system
常用SQL语句总结
Some problems encountered in recent programming 2021 / 9 / 8
双指针进阶--leetcode题目--盛最多水的容器