solution.md 4.2 KB
Newer Older
1 2
# 接雨水
<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>n == height.length</code></li>	<li><code>0 <= n <= 3 * 10<sup>4</sup></code></li>	<li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
3
<p>以下错误的选项是?</p>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> nums = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
    int res;

    res = sol.trap(nums);

    cout << res;
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    int trap(vector<int> &height)
    {
        int n = height.size(), area = 0;
        stack<pair<int, int>> st;
        for (int i = 0; i < n; i++)
        {
            if (st.empty())
                st.push(make_pair(height[i], i));
            else if (height[i] < st.top().first)
            {
                st.push(make_pair(height[i], i));
            }
            else
            {
                while (!st.empty() && height[i] >= st.top().first)
                {
                    auto tmp = st.top();
                    st.pop();

                    area += (i - 1 - st.top().second) * (min(st.top().first, height[i]) - tmp.first);
                }
                st.push(make_pair(height[i], i));
            }
        }
        return area;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    int trap(vector<int> &height)
    {
        int res = 0;
        int left = 0, left_max = 0;
        int right = height.size() - 1, right_max = 0;
        while (left < right)
        {
            if (height[left] < height[right])
            {
                if (height[left] > left_max)
                {
                    left_max = height[left];
                }
                else
                {
                    res += left_max - height[left];
                }
                left++;
            }
            else
            {
                if (height[right] > right_max)
                {
                    right_max = height[right];
                }
                else
                {
                    res += right_max - height[right];
                }
                right--;
            }
        }
        return res;
    }
};
```

### B
```cpp
class Solution
{
public:
    int trap(vector<int> &height)
    {
        int res = 0;
        for (int i = 1; i < height.size(); ++i)
        {
            int left_m = height[i], right_m = left_m;

            for (int j = 0; j < i; ++j)
                left_m = max(left_m, height[j]);

            for (int j = i + 1; j < height.size(); ++j)
                right_m = max(right_m, height[j]);
            int m = min(left_m, right_m);
            res += m > height[i] ? m - height[i] : 0;
        }
        return res;
    }
};
```

### C
```cpp
class Solution
{
public:
    int trap(vector<int> &height)
    {
        int len = height.size();
        if (len == 0)
            return 0;
        vector<int> left_max(len, 0);
        vector<int> right_max(len, 0);
        int ans = 0;

        left_max[0] = height[0];
        for (int i = 1; i < len; i++)
        {
            left_max[i] = max(height[i], left_max[i - 1]);
        }

        right_max[len - 1] = height[len - 1];
        for (int i = len - 2; i >= 0; i--)
        {
            right_max[i] = max(height[i], right_max[i + 1]);
        }

        for (int i = 0; i < len; i++)
        {
            ans += min(left_max[i], right_max[i]) - height[i];
        }

        return ans;
    }
};
```