# 接雨水

<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图，计算按此排列的柱子，下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1：</strong></p><p><img src="https://img-service.csdnimg.cn/img_convert/eb3d1efe3faa6dfe31363f4009af2a48.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>

以下程序实现了这一功能，请你填补空白处内容：

```java
class Solution {
	public int trap(int[] height) {
		if (height == null)
			return 0;
		int len = height.length;
		if (len == 0)
			return 0;
		int res = 0;
		int[] left_max = new int[len];
		int[] right_max = new int[len];
		left_max[0] = height[0];

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

		____________________;

		for (int i = 1; i < len - 1; i++) {
			res += Math.min(left_max[i], right_max[i]) - height[i];
		}
		return res;
	}
}

```

## template

```java
class Solution {
	public int trap(int[] height) {
		if (height == null)
			return 0;
		int len = height.length;
		if (len == 0)
			return 0;
		int res = 0;
		int[] left_max = new int[len];
		int[] right_max = new int[len];
		left_max[0] = height[0];

		for (int i = 1; i < len; i++) {
			left_max[i] = Math.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] = Math.max(height[i], right_max[i + 1]);
		}

		for (int i = 1; i < len - 1; i++) {
			res += Math.min(left_max[i], right_max[i]) - height[i];
		}
		return res;
	}
}

```

## 答案

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

## 选项

### A

```java
for (int i = len - 2; i >= 0; i--) {
	right_max[i] = Math.max(height[i], right_max[i]);
}
```

### B

```java
for (int i = len; i >= 0; i--) {
	right_max[i] = Math.max(height[i], right_max[i]);
}
```

### C

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