feat: 2025-10-30打卡(接雨水)
This commit is contained in:
31
42.trapping-rain-water.java
Normal file
31
42.trapping-rain-water.java
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user