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
.

To solve the problem. the key point to find the maxHeight index.
#include <vector>
#include <iostream>
#include <climits>
using namespace std;
/*
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.
*/
int trap(vector<int>& height) {
int maxHeight = 0;
int maxIndex = 0;
for(int i = 0; i < height.size(); ++i) {
if(maxHeight < height[i]) {
maxHeight = height[i];
maxIndex = i;
}
}
int water = 0;
int maxSoFar = 0;
for(int i = 0; i < <strong>maxIndex</strong>; ++i) {
if(height[i] < maxSoFar) water += (maxSoFar - height[i]); // less then maxSoFar can accumulate water.
else maxSoFar = height[i];
}
maxSoFar = 0;
for(int i = height.size() - 1; i > <strong>maxIndex</strong>; --i) {
if(height[i] < maxSoFar) water += (maxSoFar - height[i]); // less then maxSoFar can accumulate water.
else maxSoFar = height[i];
}
return water;
}
int main(void) {
vector<int> nums{0, 1, 0, 3, 1, 0, 1, 3, 2, 1, 2, 1};
cout << trap(nums) << endl;
}
Another interesting variation! Trapping rain water in matrix
/*
Suppose given a matrix
{{0, 2, 5},
{5, 4, 5},
{5, 5, 5}};
In this matrix, the max water can be trapped is: 0;
*/
#include "header.h"
using namespace std;
class Solution {
private:
vector< vector<int> > matrix;
public:
Solution(vector< vector<int> >& input) {
matrix = input;
}
int trappWaterinMatrix() {
if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
int rows = matrix.size();
int cols = matrix[0].size();
struct posIndex {
int value;
int row;
int col;
posIndex(int v, int r, int c) : value(v), row(r), col(c) {}
bool operator < (posIndex b) const {
return value > b.value;
}
};
priority_queue<posIndex, vector<posIndex> > minHeap;
vector< vector<bool> > visited(rows, vector<bool>(cols, false));
// we need to first get the minimum value from the border.
// first row and last row
for(int i = 0; i < cols; ++i) {
minHeap.push(posIndex(matrix[0][i], 0, i));
visited[0][i] = true;
minHeap.push(posIndex(matrix[rows - 1][i], rows - 1, i));
visited[rows - 1][i] = true;
}
// first column and last column
for(int i = 1; i < rows; ++i) {
minHeap.push(posIndex(matrix[i][0], i, 0));
visited[i][0] = true;
minHeap.push(posIndex(matrix[i][cols -1], i, cols -1));
visited[i][cols - 1] = true;
}
int waterTrapped = 0;
// bfs traversal the matrix to caculate the trapped water.
vector< vector<int> > dir{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
while(!minHeap.empty()) {
auto curr = minHeap.top(); minHeap.pop();
for(int i = 0; i < dir.size(); ++i) {
posIndex next(0, 0, 0);
next.row = curr.row + dir[i][0];
next.col = curr.col + dir[i][1];
if(next.row >= 0 && next.row < rows && next.col >= 0 && next.col < cols && !visited[next.row][next.col]) {
visited[next.row][next.col] = true;
next.value = max(curr.value, matrix[next.row][next.col]);
minHeap.push(next);
waterTrapped += max(0, curr.value - matrix[next.row][next.col]);
}
}
}
return waterTrapped;
}
};