{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "39ad92901f2543a585b47c5dcf7731c4",
  "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": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int trap(int[] height) {\n\t\tif (height == null)\n\t\t\treturn 0;\n\t\tint len = height.length;\n\t\tif (len == 0)\n\t\t\treturn 0;\n\t\tint res = 0;\n\t\tint[] left_max = new int[len];\n\t\tint[] right_max = new int[len];\n\t\tleft_max[0] = height[0];\n\n\t\tfor (int i = 1; i < len; i++) {\n\t\t\tleft_max[i] = Math.max(height[i], left_max[i - 1]);\n\t\t}\n\t\tright_max[len - 1] = height[len - 1];\n\n\t\tfor (int i = len - 2; i >= 0; i--) {\n\t\t\tright_max[i] = Math.max(height[i], right_max[i + 1]);\n\t\t}\n\n\t\tfor (int i = 1; i < len - 1; i++) {\n\t\t\tres += Math.min(left_max[i], right_max[i]) - height[i];\n\t\t}\n\t\treturn res;\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-f08ed7a411b948daa662d0e8cd0a0948",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470120"
}