leetcode 高效制胜 14 生活趣题

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;
    }
};
  1. 找最靠近左边最高的,和找最靠近右边最高的
  2. 所以从左往右有一个递增序列
  3. 从右往左有一个递增序列
  4. 如果某一边比较小,因为它已经找到了比它高的最远的另一边,所以它可以往中间移动直到比现在高
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;
    }
};
  1. 栈中递减,当右边大于栈顶,弹出栈顶,并补充雨水。原理是栈顶左右都比它高,所以存在接雨水的空间

双指针

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;
    }
};
  1. 和上面一题差不多
  2. 找左边最高的,找右边最高的,就是当前能达到的最大高度

双指针

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. 左边最高、右边最高
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值