当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢

国内外最好的12款项目管理系统优劣势分析

Le roman du drapeau de l'imitation haute version flutter, apprenez - le

Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad

Flutter中的GetX状态管理用起来真的那么香吗?

牛客网:接雨水的双指针问题

项目中常用到的 19 条 MySQL 优化

Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)

WebService client request failed can not create a secure xmlinputfactory
Several cases of index invalidation caused by MySQL

Golang 类型断言
随机推荐
三款很酷很骚气的底部导航
2022 point de connaissance de l'examen des ingénieurs en sécurité de l'information: contrôle d'accès
牛客网:接雨水的双指针问题
【Try to Hack】masscan
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
Which securities dealers recommend? Is online account opening safe?
A person even ran a Weibo app
根据先序遍历和中序遍历生成后序遍历
Four traversals of map sets
PyQt5_ Qtablewidget paging radio right-click menu control
3D打印和激光切割流程的初步了解
Come on, touch and write a hook
Math. Max() method obtains the maximum value in the array and returns Nan problem analysis
E: Unable to acquire lock /var/lib/dpkg/lock
Big guy on security privacy computing escort data security needs to pay attention to science and technology ethics at the same time
2022山东健博会,济南国际大健康产业博览会,中国营养健康展
Activity的onSaveInstanceState回调时机
再见,2020,这碗毒鸡汤,我先干了
ACM. HJ89 24点运算 ●●●
Kotlin coroutine asynchronous flow