https://leetcode.cn/problems/trapping-rain-water/description/
class Solution {
public:int trap(vector<int>& height) {int n = height.size();// 预处理数组vector<int> lefts(n, 0);vector<int> rights(n, 0);// 预处理记录左侧最大值lefts[0] = height[0];for (int i = 1; i < n; ++i) {lefts[i] = max(lefts[i-1], height[i]);}// 预处理记录右侧最大值rights[n-1] = height[n-1];for (int i = n - 2; i > 0; --i) {rights[i] = max(rights[i+1], height[i]);}// 将每个柱子可以接到水的值进行累加int ret = 0;for (int i = 0; i < n; ++i) {if (min(lefts[i], rights[i]) - height[i] > 0)ret += min(lefts[i], rights[i]) - height[i];} return ret;}
};