intSolution::trap(const vector<int> &A){ int l = 0, r = A.size() - 1, res = 0, mi = 0; while(l < r) { if(A[l] > mi and A[r] > mi) { res += (r - l + 1) * (min(A[l],A[r]) - mi); mi = min(A[l],A[r]); res -= mi; if(A[l] < A[r]) l++; else r--; } elseif(A[l] <= mi and A[r] >= mi) { res -= A[l++]; } elseif(A[r] <= mi and A[l] >= mi) { res -= A[r--]; } } return res - mi; }