class Solution {
public int trap(int[] height) {
int n = height.length;
int left = 0;
int right = n - 1;
int left_max = 0;
int right_max = 0;
int ans = 0;
while(left <= right) {
if(left_max <= right_max) {
left_max = Math.max(left_max, height[left]);
ans += left_max - height[left];
left++;
}
else {
right_max = Math.max(right_max, height[right]);
ans += right_max - height[right];
right--;
}
}
return ans;
}
}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)