42 Trapping rain water
https://leetcode.com/problems/trapping-rain-water/
跟11的思路很像,初见杀很难想到吧。
class Solution {
public int trap(int[] height) {
int leftMax = 0;
int rightMax = 0;
int i = 0;
int j = height.length-1;
int sum = 0;
while (i <= j) {
leftMax = Math.max(leftMax, height[i]);
rightMax = Math.max(rightMax, height[j]);
if (leftMax < rightMax) {
// i左边的bar已经探索完了,leftMax确定是它的短板了,所以它能装的水就固定了
sum += leftMax - height[i++];
} else {
sum += rightMax - height[j--];
}
}
return sum;
}
}更容易想到的思路是mono stack,但是要记得mono stack只能找到第一个大的值,但是能装多少取决于最大值,所以思路是固定水的底,然后一层一层往上加,举个例子就


Last updated