当前位置:网站首页>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
边栏推荐
- Model problems of stock in and stock out and inventory system
- 【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- Shell - introduction, variables, and basic syntax
- C dapper basically uses addition, deletion, modification and query transactions, etc
- Websocket (basic)
- Baidu Map 3D rotation and tilt angle adjustment
- Collection of common SQL statements
- On lambda powertools typescript
- node中,如何手动实现触发垃圾回收机制
猜你喜欢
线性代数感悟之2
Qt 修改UI没有生效
flink 学习(十二)Allowed Lateness和 Side Output
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Use of todesk remote control software
For the space occupation of the software, please refer to the installation directory
Detailed explanation of Milvus 2.0 quality assurance system
Scope and scope chain in JS
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
groutine
随机推荐
. net cross platform principle (Part I)
索引:手把手教你索引从零基础到精通使用
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
1-3 components and modules
EF core in ASP Generate core priority database based on net entity model
El date picker limits the selection range from the current time to two months ago
开期货,开户云安全还是相信期货公司的软件?
In embedded system, must the program code in flash be moved to ram to run?
How to manually implement the mechanism of triggering garbage collection in node
If you start from zero according to the frame
RPC核心概念理解
[simple understanding of database]
Use of five routing guards
JVM类加载机制
[C#] 彻底搞明白深拷贝
Lock lock
Detailed explanation of C webpai route
Shell - introduction, variables, and basic syntax
JS to find the character that appears three times in the string
[ES6] promise related (event loop, macro / micro task, promise, await / await)