当前位置:网站首页>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
边栏推荐
- 索引:手把手教你索引从零基础到精通使用
- Milvus 2.0 質量保障系統詳解
- Generating access keys using JSON webtoken
- 【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
- C语言程序设计之函数的构造
- How to manually implement the mechanism of triggering garbage collection in node
- stm32入门开发板选野火还是正点原子呢?
- 为什么有些人说单片机简单,我学起来这么吃力?
- El cascade and El select click elsewhere to make the drop-down box disappear
- Understanding of RPC core concepts
猜你喜欢
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
EF core in ASP Generate core priority database based on net entity model
Solution architect's small bag - 5 types of architecture diagrams
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Collection of common SQL statements
[WPF binding 3] listview basic binding and data template binding
Webapi + form form upload file
Lock lock
Milvus 2.0 質量保障系統詳解
groutine
随机推荐
Signalr can actively send data from the server to the client
Shell-sed命令的使用
Document operation II (5000 word summary)
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Further optimize Baidu map data visualization
RPC核心概念理解
Understanding and small examples of unity3d object pool
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
1-3 components and modules
Solution architect's small bag - 5 types of architecture diagrams
Shell-sort命令的使用
Collection of common SQL statements
[batch change MySQL table and corresponding codes of fields in the table]
For the space occupation of the software, please refer to the installation directory
Indexes and views in MySQL
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
Websocket (basic)
Net standard
ASP. NET CORE3. 1. Solution to login failure after identity registers users
Simulation of infrared wireless communication based on 51 single chip microcomputer