当前位置:网站首页>牛客网:接雨水的双指针问题
牛客网:接雨水的双指针问题
2022-06-23 22:12:00 【lsgoose】
1.盛水最多的容器



思路如下所示:
设置两个指针,从两边向中间靠拢,每次移动height较小的一端。
代码如下所示:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param height int整型vector
* @return int整型
*/
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.接雨水问题


双指针
这题其实和上边那道差不太多。首先给定一个区间,我们要知道,决定这个区间每个值对应的水量是和左右边界谁小决定的。
因此,我们维护一个maxL和maxR记录包含左右区间的最大值,用于计算水量,而计算的过程是我们维护一个[left, right]的双指针,由两侧向中间靠拢,每次将小的移动,因为这样我们才能计算出最大的接水量,中间的高度可能更高。
代码如下所示:
class Solution {
public:
/**
* max water
* @param arr int整型vector the array
* @return long长整型
*/
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;
}
};单调栈
这题也很适合用单调栈来解决,单调栈即维护一个单调序列的栈,这里我们维护一个递减的下标栈。
这里单调栈模拟的是对于一个区间一层一层加水的过程。具体可以参考leetcode上面的思路:
代码如下所示:
class Solution {
public:
/**
* max water
* @param arr int整型vector the array
* @return long长整型
*/
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();
//说明之前的都比他小 这个高度的对应位置装不了水
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;
}
};边栏推荐
- PyQt5_ Qtablewidget paging radio right-click menu control
- HAOGE's blog Road
- Isolement des transactions MySQL
- 7、STM32——LCD
- PHP的curl功能扩展基本用法
- Avoid confusion when switching between arouter components
- ORB_SLAM3环境搭建及demo演示
- Zynq ultrascale+ RF data converter IP configuration - ADC
- Summary of cloud native pipeline tools
- CTF—Go题目复现
猜你喜欢

Bilibili × Blue bridge cloud course | online programming practice competition is new!

The sandbox week is coming!

Some explanations of Tim timer of embedded interface and STM32 template library function of NVIC

STM32------ADC(电压检测)

How can wechat video numbers be broadcast live on a PC?

Ambire Guide: the arbitrum odyssey begins! Week 1 - Cross Chain Bridge

腾讯会议号设计的几种猜测

STM32 ------ external interrupt

Sorry, your USB cable may be wrong!

“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛
随机推荐
How does the fortress connection key server associate with the server host?
根据先序遍历和中序遍历生成后序遍历
百万消息量IM系统技术要点分享
ASM文件系统 数据如何写和读数据
Postman可以集成到CI,CD流水线中做自动化接口测试吗?
"Shanda Diwei Cup" the 12th Shandong ICPC undergraduate program design competition
Telecommuting: how to become a master of time management| Community essay solicitation
《阿里云天池大赛赛题解析》——O2O优惠卷预测
Detailed quaternion
MySQL事务隔离
光大期货安全吗?开户需要什么东西?
PyQt5_ Qtablewidget paging radio right-click menu control
Avoid confusion when switching between arouter components
CTF—Go题目复现
不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!...
远程办公之:如何成为时间管理大师?| 社区征文
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
Common core resource objects of kubernetes
MySQL导致索引失效的几种情况
The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil