11. 盛最多水的容器 【双指针】
给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器。
示例 1:
输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
示例 2:
输入:height = [1,1]
输出:1
示例 3:
输入:height = [4,3,2,1,4]
输出:16
示例 4:
输入:height = [1,2,1]
输出:2
提示:
n = height.length
2 <= n <= 3 * 104
0 <= height[i] <= 3 * 104
class Solution {
public:
int maxArea(vector<int>& height) {
int n=height.size()-1;;
int l=0,r=n;
int ans=0;
while(l<r){
ans=max(ans,(r-l)*min(height[l],height[r]));
if(height[l]<height[r]){
int i=l;
while(i<r&&height[i]<=height[l])i++;
l=i;
}else{
int j=r;
while(j>l&&height[j]<=height[r])j--;
r=j;
}
}
return ans;
}
};
- 找最靠近左边最高的,和找最靠近右边最高的
- 所以从左往右有一个递增序列
- 从右往左有一个递增序列
- 如果某一边比较小,因为它已经找到了比它高的最远的另一边,所以它可以往中间移动直到比现在高
class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size() - 1;
int ans = 0;
while (l < r) {
int area = min(height[l], height[r]) * (r - l);
ans = max(ans, area);
if (height[l] <= height[r]) {
++l;
}
else {
--r;
}
}
return ans;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/container-with-most-water/solution/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
42. 接雨水【动态规划,双指针,单调栈】
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
提示:
n == height.length
0 <= n <= 3 * 104
0 <= height[i] <= 105
单调栈
class Solution {
public:
int trap(vector<int>& height) {
stack<int> myStack;
int ans=0;
for(int i=0;i<height.size();i++){
while(myStack.size()&&height[i]>height[myStack.top()]){
int l=myStack.top();
myStack.pop();
if(myStack.size()){
ans+=(i-myStack.top()-1)*(min(height[i],height[myStack.top()])-height[l]);
}
}
myStack.push(i);
}
return ans;
}
};
- 栈
- 栈中递减,当右边大于栈顶,弹出栈顶,并补充雨水。原理是栈顶左右都比它高,所以存在接雨水的空间
双指针
class Solution {
public:
int trap(vector<int>& height) {
int l=0;
int r=height.size()-1;
int ans=0;
while(l<r){
if(height[l]<height[r]){
int leftMax=height[l];
while(l<r&&height[l]<=leftMax){
ans+=leftMax-height[l];
l++;
}
}else{
int rightMax=height[r];
while(l<r&&height[r]<=rightMax){
ans+=rightMax-height[r];
r--;
}
}
}
return ans;
}
};
- 和上面一题差不多
- 找左边最高的,找右边最高的,就是当前能达到的最大高度
双指针
class Solution {
public int trap(int[] height) {
int ans = 0;
int left = 0, right = height.length - 1;
int leftMax = 0, rightMax = 0;
while (left < right) {
leftMax = Math.max(leftMax, height[left]);
rightMax = Math.max(rightMax, height[right]);
if (height[left] < height[right]) {
ans += leftMax - height[left];
++left;
} else {
ans += rightMax - height[right];
--right;
}
}
return ans;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode-solution-tuvc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
动态规划
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
if (n == 0) {
return 0;
}
vector<int> leftMax(n);
leftMax[0] = height[0];
for (int i = 1; i < n; ++i) {
leftMax[i] = max(leftMax[i - 1], height[i]);
}
vector<int> rightMax(n);
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; --i) {
rightMax[i] = max(rightMax[i + 1], height[i]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += min(leftMax[i], rightMax[i]) - height[i];
}
return ans;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode-solution-tuvc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 左边最高、右边最高