- - 嗯, 水题
const int MAXN = 1e6 + 10;
int dleft[MAXN], dright[MAXN];
class Solution {
public:
int trap(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= 0)
return 0;
dleft[0] = 0;
for(int i = 1; i < n; ++ i)
dleft[i] = max(dleft[i - 1], A[i - 1]);
dright[n - 1] = 0;
for(int i = n - 2; i >= 0; -- i)
dright[i] = max(dright[i + 1], A[i + 1]);
int ans = 0;
for(int i = 0; i < n; ++ i){
int cur = min(dleft[i],dright[i]);
if(cur > A[i])
ans += cur - A[i];
}
return ans;
}
};