方法一:
暴力 —>超時
class Solution {
int findLeftMaxHeight(vector<int>& height, int center){
int res = 0;
for(int i = center - 1; i >=0; i--){
res = max(res, height[i]);
}
return res;
}
int findRightMaxHeight(vector<int>& height, int center){
int res = 0;
for(int i = center + 1; i < height.size(); i++){
res = max(res, height[i]);
}
return res;
}
public:
int trap(vector<int>& height) {
int n = height.size();
if(n < 3) return 0;
//每個點能夠接的雨水數量:min(leftMaxHeight, rightMaxHeight)
int res = 0;
//開頭和尾巴都不能接雨水
for(int i = 1; i < n-1; i++){
int leftMaxHeight = findLeftMaxHeight(height,i);
int rightMaxHeight = findRightMaxHeight(height,i);
if(height[i] < min(leftMaxHeight,rightMaxHeight)){
res += min(leftMaxHeight,rightMaxHeight) - height[i];
}
}
return res;
}
};
方法二:
上述方法,在每一次計算左邊和右邊的最高高度的時候,有重複的操作。
解決方法:用“空間換時間”。我們可以通過一次遍歷,把已經掃過的柱形高度的最大值記錄下來,具體如下:
1、從左向右:記錄當前遍歷位置左側的最高高度。
2、從右向左:記錄當前遍歷位置右側的最高高度。
注意邊界:最左邊位置和最右邊位置的最高高度是 0。
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
if(n < 3) return 0;
//每個點能夠接的雨水數量:min(leftMaxHeight, rightMaxHeight)
int res = 0;
vector<int> left(n,0);
vector<int> right(n,0);
// 記錄了索引位置 i 左邊的最高高度,不包括索引 i
for(int i = 1; i < n; i++){
left[i] = max(left[i-1], height[i-1]);
}
// 記錄了索引位置 i 右邊的最高高度,不包括索引 i
for(int i = n - 2; i >= 0; i--){
right[i] = max(right[i+1],height[i+1]);
}
//開頭和尾巴都不能接雨水
for(int i = 1; i < n-1; i++){
int leftMaxHeight = left[i];
int rightMaxHeight = right[i];
if(height[i] < min(leftMaxHeight,rightMaxHeight)){
res += min(leftMaxHeight,rightMaxHeight) - height[i];
}
}
return res;
}
};
方法三:
使用雙指針記錄左右兩邊的最大值
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
if(n < 3) return 0;
//每個點能夠接的雨水數量:min(leftMaxHeight, rightMaxHeight)
int res = 0;
int leftMaxHeight = height[0];
int rightMaxHeight = height[n-1];
int left = 1, right = n - 1;
//注意是等號
while(left <= right){
int minVal = min(leftMaxHeight,rightMaxHeight);
if(minVal == leftMaxHeight){ //最小值在左邊,該點的存水量取決於左邊的最大值
if(minVal > height[left]){
res += minVal - height[left];
}
leftMaxHeight = max(leftMaxHeight,height[left]);
left++;
}else{
if(minVal > height[right]){
res += minVal - height[right];
}
rightMaxHeight = max(rightMaxHeight,height[right]);
right--;
}
}
return res;
}
};