leetcode 11. Container With Most Water& lintcode 363. 接雨水

愚蠢如我

这两个其实还不太一样的

和这个http://acm.hdu.edu.cn/showproblem.php?pid=1506 更不一样(后者是单调栈)

https://www.lintcode.com/problem/trapping-rain-water/description

由于图是酱婶的,所以需要一位一位的算

class Solution {
public:
    /**
     * @param heights: a list of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        // write your code here
        int len = heights.size();
        if (len <= 2) {
            return 0;
        }
        int l = 1;
        int r = len - 2;
        int tot = 0;
        int lmax = heights[0];
        int rmax = heights[r + 1];
        while (l <= r) {
            if (lmax < rmax) {
                if (lmax < heights[l]){
                    lmax = heights[l];
                } else {
                    tot += (lmax - heights[l]);
                }
                l ++;
            } else {
                if (rmax < heights[r]) {
                    rmax = heights[r];
                } else {
                    tot += (rmax - heights[r]);
                }
                r--;
            }
        }
        return tot;
    }
};

事实证明我昨天晚上睡觉前想的 除了空间复杂度高 也不是不能A

class Solution {
public:
    /**
     * @param heights: a list of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        // write your code here
        if (heights.size() == 0 || heights.size() == 1) {
            return 0;
        }
        int len = heights.size();
        vector<int> v1(heights);
        vector<int> v2(heights);
        int maxn = heights[0];
        v1[0] = 0;
        for (int i = 1; i < len; i ++) {
            v1[i] = maxn;
            if (maxn < heights[i])
                maxn = heights[i];
        }
        for (int i = 0; i < len; i ++)
            printf("i=%d,v1[i]=%d\n", i, v1[i]);
        v2[len - 1] = 0;
        maxn = heights[len - 1];
        for (int i = len - 2; i >= 0; i --) {
            v2[i] = maxn;
            if (maxn < heights[i])
                maxn = heights[i];
        }
        for (int i = 0; i < len; i ++)
            printf("i=%d,v2[i]=%d\n", i, v2[i]);
        int tot = 0;
        for (int i = 1; i < len - 1;i ++) {
            tot += max(0, min(v1[i], v2[i]) - heights[i]);
            printf("i= %d, tot = %d\n", i, max(0, min(v1[i], v2[i]) - heights[i]));
        }
        return tot;
    }
};

回来看leetcode

图是这样以为着 是找两个隔板间的矩形

相同的地方都是两个指针选小的往中间走

class Solution {
public:
    int maxArea(vector<int>& height) {
        int len = height.size();
        if (len <= 1) {
            return 0;
        }
        int l = 0;
        int r = len - 1;
        int tot = 0;
        while (l < r) {
            tot = max(tot, (r-l)*min(height[r], height[l]));
            if (height[r] < height[l]) {
                r--;
            } else {
                l++;
            }
        }
        return tot;
    }
};

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值