{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "b0bdaa3932ce4195b936e641c3f715fe",
  "keywords": "栈,数组,双指针,动态规划,单调栈",
  "title": "接雨水",
  "desc": [
    {
      "content": "\n<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>\t<li><code>n == height.length</code></li>\t<li><code>0 <= n <= 3 * 10<sup>4</sup></code></li>\t<li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint trap(vector<int> &height)\n\t{\n\t\tint res = 0;\n\t\tint left = 0, left_max = 0;\n\t\tint right = height.size() - 1, right_max = 0;\n\t\twhile (left < right)\n\t\t{\n\t\t\tif (height[left] < height[right])\n\t\t\t{\n\t\t\t\tif (height[left] > left_max)\n\t\t\t\t{\n\t\t\t\t\tleft_max = height[left];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres += left_max - height[left];\n\t\t\t\t}\n\t\t\t\tleft++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (height[right] > right_max)\n\t\t\t\t{\n\t\t\t\t\tright_max = height[right];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres += right_max - height[right];\n\t\t\t\t}\n\t\t\t\tright--;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-379fe0110ce244ed9109860976c01206",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470120"
}