mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-07 10:40:36 +00:00
34 lines
670 B
C++
34 lines
670 B
C++
#include <vector>
|
|
using namespace std;
|
|
|
|
class Solution {
|
|
public:
|
|
int trap(std::vector<int> &height) {
|
|
|
|
if (height.size() < 3) {
|
|
return 0;
|
|
}
|
|
int n = height.size();
|
|
int l = 0, r = n - 1;
|
|
|
|
int lmax = 0;
|
|
int rmax = 0;
|
|
|
|
long long totw = 0;
|
|
while (l < r) {
|
|
lmax = std::max(lmax, height[l]);
|
|
rmax = std::max(rmax, height[r]);
|
|
if (lmax < rmax) {
|
|
totw += lmax - height[l];
|
|
l++;
|
|
} else {
|
|
totw += rmax - height[r];
|
|
r--;
|
|
}
|
|
}
|
|
return totw;
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
} |