int trap(int* height, int heightSize){
int sstack[heightSize];
int top = 0;
int height_min;
int bound_height;
int cnt = 0;
for (int i = 0; i < heightSize; i++) {
while((top > 0) && (height[sstack[top - 1]] < height[i])) {
height_min = height[sstack[top - 1]];
top--;
if (top <= 0) {
break;
}
if (height[sstack[top - 1]] > height[i]) {
bound_height = height[i];
} else {
bound_height = height[sstack[top - 1]];
}
cnt = cnt + (bound_height - height_min) * (i - sstack[top - 1] - 1);
}
sstack[top] = i;
top++;
}
return cnt;
}
力扣42. 接雨水(单调栈)
最新推荐文章于 2024-11-08 23:27:36 发布