题目
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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
class Solution {
public:
int trap(int A[], int n) {
}
};
给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。
Paste_Image.png
样例
如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返回 6.
分析
有三种方法思路可以解决
方法一:
首先从左遍历一遍,求出每个点的左边的最高点数值(从最左边的那个点开始,到目前这个点为止的所有点中的最高高度)。
再从右边遍历一遍,求出每个点的右边的最高点数值(从最右边的那个点开始,到目前这个点为止的所有点中的最高高度)。
要能够接住水,就要求该点的左右两边的两个最高点数值(上面两次遍历求到的)中,最小的那个数值(也就是高度)应该比中间的要大。而能接住的水的面积就是这两个数字之差。
public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] A) {
if(A == null || A.length < 3)
return 0;
int localMax = A[0];
int[] left = new int[A.length];
int[] right = new int[A.length];
for(int i=0;i<A.length;i++) {
if(A[i] <= localMax)
left[i] = localMax;
else {
localMax = A[i];
left[i] = localMax;
}
}
localMax = A[A.length-1];
for(int i=A.length-1;i>-1;i--) {
if(A[i] <= localMax)
right[i] = localMax;
else {
localMax = A[i];
right[i] = localMax;
}
}
int area = 0;
for(int i=0;i<A.length;i++) {
area += Math.min(left[i], right[i]) - A[i];
}
return area;
}
}
方法二
首先遍历一遍找到最高点,然后从左右两边分别向最高点进。
注:这里最高点相当于最高的隔板:对于位于最高隔板左边的板子来说,只要它左边相邻的那块板子高度比它高,就能存水(不用考虑该板子右边相邻的板子的高度,因为最高隔板保证了水会被关住)。
而对于位于最高隔板右边边的板子来说,只要它右边相邻的那块板子高度比它高,就能存水(不用考虑该板子左边相邻的板子的高度,因为最高隔板保证了水会被关住)。
public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] heights) {
if(heights.length <= 2) return 0;
int max = -1, maxInd = 0;
int i = 0;
for(; i < heights.length; ++i){
if(heights[i] > max){
max = heights[i];
maxInd = i;
}
}
int area = 0, root = heights[0];
for(i = 0; i < maxInd; ++i){
if(root < heights[i]) root = heights[i];
else area += (root - heights[i]);
}
for(i = heights.length-1, root = heights[heights.length-1]; i > maxInd; --i){
if(root < heights[i]) root = heights[i];
else area += (root - heights[i]);
}
return area;
}
}
方法三
左右两个指针,一头一尾开始遍历,首先找到自己这一边的局部最高点,只要比最高点小就可以装水。
这个和方法1类似。
public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] A) {
if(A == null || A.length < 3)
return 0;
//两根指针一头一尾向中间进发
int left = 0;
int right = A.length-1;
//两个变量存储左右两边的局部最大高度
int leftMax = 0;
int rightMax = 0;
int area = 0;
while(left <= right) {
leftMax = Math.max(leftMax, A[left]);
rightMax = Math.max(rightMax, A[right]);
//小的那边可以存水
if(leftMax <= rightMax) {
if(leftMax > A[left])
area += leftMax - A[left];
left++;
}
else {
if(rightMax > A[right])
area += rightMax - A[right];
right--;
}
}
return area;
}
}