feat: 2025-10-30打卡(接雨水)

This commit is contained in:
wu xiangkai
2025-10-30 14:47:22 +08:00
parent ea9dfb9d54
commit b897d019fa
3 changed files with 149 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
/*
* @lc app=leetcode id=42 lang=java
*
* [42] Trapping Rain Water
*/
// @lc code=start
class Solution {
public int trap(int[] height) {
int lmax = height[0], rmax = height[height.length-1];
int l = 0, r = height.length-1, cap = 0;
while(l <= r) {
if(lmax < rmax) {
cap += lmax - height[l];
l++;
if(l < height.length) {
lmax = Math.max(lmax, height[l]);
}
} else {
cap += rmax - height[r];
r--;
if(r >=0) {
rmax = Math.max(rmax, height[r]);
}
}
}
return cap;
}
}
// @lc code=end