当前位置:网站首页>Niuke.com: the double pointer problem of receiving rainwater
Niuke.com: the double pointer problem of receiving rainwater
2022-06-23 23:42:00 【lsgoose】
1. The container with the most water



The idea is as follows :
Set two pointers , From both sides to the middle , Each move height Smaller end .
The code is as follows :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param height int integer vector
* @return int integer
*/
int maxArea(vector<int>& height) {
// write code here
if(height.size()<0){
return 0;
}
int res=0;
int left=0, right=height.size()-1;
while(left<right){
res=max(res, min(height[left], height[right])*(right-left));
if(height[left]>height[right]){
right--;
}else{
left++;
}
}
return res;
}
};2. The rain problem


Double pointer
This question is not much different from the one above . First, let's give an interval , We need to know , The amount of water corresponding to each value in this interval is determined by the smaller of the left and right boundaries .
therefore , We maintain a maxL and maxR Record the maximum value of the left and right intervals , Used to calculate water volume , And the process of computing is that we maintain a [left, right] Double pointer , From both sides to the middle , Move small at a time , Because in this way, we can calculate the maximum water receiving capacity , The middle height may be higher .
The code is as follows :
class Solution {
public:
/**
* max water
* @param arr int integer vector the array
* @return long Long integer
*/
long long maxWater(vector<int>& arr) {
if(arr.size()==0){
return 0;
}
long long res=0;
int left=0, right=arr.size()-1;
int maxL=0, maxR=0;
while(left<right){
maxL=max(maxL, arr[left]);
maxR=max(maxR, arr[right]);
if(maxR>maxL){
res += maxL-arr[left++];
}else{
res += maxR-arr[right--];
}
}
return res;
}
};Monotonic stack
This problem is also suitable for solving with monotone stack , A monotone stack is a stack that maintains a monotone sequence , Here we maintain a decreasing subscript stack .
Here, the monotone stack simulates the process of adding water layer by layer for an interval . For details, please refer to leetcode The above ideas :
The code is as follows :
class Solution {
public:
/**
* max water
* @param arr int integer vector the array
* @return long Long integer
*/
long long maxWater(vector<int>& arr) {
if(arr.size()==0){
return 0;
}
long long res=0;
stack<int> st;
for(int i=0;i<arr.size();++i){
while(!st.empty() && arr[i]>arr[st.top()]){
int top=st.top();
st.pop();
// It means that the previous ones are younger than him The corresponding position at this height cannot hold water
if(st.empty()){
break;
}
int left=st.top();
int width=i-left-1;
int height=min(arr[left], arr[i])-arr[top];
res+=width*height;
}
st.push(i);
}
return res;
}
};边栏推荐
- 不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!...
- 思考(八十六):主从实现思路
- 文言文能编程???
- Why can't the netherworld fortress machine be remotely connected to the server? What are the ways to solve such problems?
- Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)
- PyQt5_QTableWidget分页单选右键菜单控件
- A cartoon reading app highly imitating Tencent comics
- 在OpenCloudOS使用snap安装.NET 6
- 一个人竟然撸了一个网易云音乐云村
- 对不起,你的USB走线可能搞错了!
猜你喜欢

Is the geTx status management in the flutter really so good to use?

PyQt5_QTableWidget分页单选右键菜单控件

HAOGE's blog Road

ORB_ Slam3 environment setup and demo demonstration

6. STM32 - serial port data transceiver Foundation

AutoCAD -- summarize three methods of drawing rounded corners in CAD

The sandbox week is coming!

High imitation Book flag novel flutter edition, learn it

Graph theory (tree diameter)

19 MySQL optimizations commonly used in projects
随机推荐
一个人竟然撸了一个微博 APP
Some explanations of Tim timer of embedded interface and STM32 template library function of NVIC
Bitmap加载内存分析
Million message IM system technical points sharing
一个人竟然撸了一个网易云音乐云村
《阿里云天池大赛赛题解析》——O2O优惠卷预测
Sorry, your USB cable may be wrong!
一款高仿腾讯漫画的漫画阅读类 APP
HDLBits-&gt;Circuits-&gt;Arithmetic Circuitd-&gt;3-bit binary adder
网站如何在Google建立索引
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
Task queue of laravel
【Try to Hack】masscan
解决Slf4j日志不打印问题
Bilibili × Blue bridge cloud course | online programming practice competition is new!
高仿書旗小說 Flutter 版,學起來
1004. 最大连续1的个数 III ●●
Several guesses about the design of Tencent conference number
思考(八十七):协议加密与压缩
PyQt5_QTableWidget分页单选右键菜单控件