[LintCode] Trapping Rain Water 收集雨水

简介: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Challenge 

O(n) time and O(1) memory

O(n) time and O(n) memory is also acceptable.

LeetCode上的原题,请参见我之前的博客Trapping Rain Water

解法一:

class Solution {
public:
    /**
     * @param heights: a vector of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        int res = 0, mx = 0, n = heights.size();
        vector<int> dp(n, 0);
        for (int i = 0; i < n; ++i) {
            dp[i] = mx;
            mx = max(mx, heights[i]);
        }
        mx = 0;
        for (int i = n - 1; i >= 0; --i) {
            dp[i] = min(dp[i], mx);
            mx = max(mx, heights[i]);
            if (dp[i] > heights[i]) res += dp[i] - heights[i];
        }
        return res;
    }
};

解法二:

class Solution {
public:
    /**
     * @param heights: a vector of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        int res = 0, l = 0, r = heights.size() - 1;
        while (l < r) {
            int mn = min(heights[l], heights[r]);
            if (mn == heights[l]) {
                ++l;
                while (l < r && heights[l] < mn) {
                    res += mn - heights[l++];
                }
            } else {
                --r;
                while (l < r && heights[r] < mn) {
                    res += mn - heights[r--];
                }
            }
        }
        return res;
    }
};

解法三:

class Solution {
public:
    /**
     * @param heights: a vector of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        int res = 0, l = 0, r = heights.size() - 1, level = 0;
        while (l < r) {
            int lower = heights[(heights[l] < heights[r]) ? l++ : r--];
            level = max(level, lower);
            res += level - lower;
        }
        return res;
    }
};

本文转自博客园Grandyang的博客,原文链接: 收集雨水[LintCode] Trapping Rain Water,如需转载请自行联系原博主。

相关文章
|
C++
【PAT甲级 - C++题解】1108 Finding Average
【PAT甲级 - C++题解】1108 Finding Average
72 0
LeetCode 407. Trapping Rain Water II
我们把最外面的四边当成四面墙,想象海水面不断的升高,首先会浸过墙面最低的格子,如果墙面最低格子的四周(出了在墙面的格子)有比它矮的格子,那么这就可以形成一个蓄水池,蓄水池的最高高度就是墙面最低的格子,于是我们计算这个蓄水池可以获得的蓄水量;然后这个蓄水池被添加到墙面中;继续在墙面中找最低的格子;
98 0
LeetCode 407. Trapping Rain Water II
|
索引
LeetCode 42 Trapping Rain Water
给定n个非负整数,每个非负整数表示一个宽度为1的柱子,计算下雨后能够捕获多少水.
71 0
LeetCode 42 Trapping Rain Water
PAT甲级 1004. Counting Leaves(30分)
PAT甲级 1004. Counting Leaves(30分)
66 0
|
人工智能 Windows
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
97 0
|
人工智能 BI
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
133 0
|
机器学习/深度学习
AtCoder Beginner Contest 218 F - Blocked Roads (最短路径还原 思维)
AtCoder Beginner Contest 218 F - Blocked Roads (最短路径还原 思维)
99 0