Trapping Rain Water — LeetCode #42
Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5]
Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
Solutions:
Python
class Solution:
def trap(self, height: List[int]) -> int:
if not height:
return 0
n = len(height)
left, right = 0, n - 1
left_max, right_max = height[left], height[right]
ans = 0
while left < right:
if left_max < right_max:
ans += left_max - height[left]
left += 1
left_max = max(left_max, height[left])
else:
ans += right_max - height[right]
right -= 1
right_max = max(right_max, height[right])
return ans
C#
class Solution {
public int Trap(int[] height) {
if (height.Length == 0) {
return 0;
}
int n = height.Length;
int left = 0, right = n - 1;
int leftMax = height[left], rightMax = height[right];
int ans = 0;
while (left < right) {
if (leftMax < rightMax) {
ans += leftMax - height[left];
left++;
leftMax = Math.Max(leftMax, height[left]);
} else {
ans += rightMax - height[right];
right--;
rightMax = Math.Max(rightMax, height[right]);
}
}
return ans;
}
}
Java
class Solution {
public int trap(int[] height) {
if (height.length == 0) {
return 0;
}
int n = height.length;
int left = 0, right = n - 1;
int leftMax = height[left], rightMax = height[right];
int ans = 0;
while (left < right) {
if (leftMax < rightMax) {
ans += leftMax - height[left];
left++;
leftMax = Math.max(leftMax, height[left]);
} else {
ans += rightMax - height[right];
right--;
rightMax = Math.max(rightMax, height[right]);
}
}
return ans;
}
}
Javascript
/**
* @param {number[]} height
* @return {number}
*/
var trap = function(height) {
if (height.length === 0) {
return 0;
}
let n = height.length;
let left = 0, right = n - 1;
let leftMax = height[left], rightMax = height[right];
let ans = 0;
while (left < right) {
if (leftMax < rightMax) {
ans += leftMax - height[left];
left++;
leftMax = Math.max(leftMax, height[left]);
} else {
ans += rightMax - height[right];
right--;
rightMax = Math.max(rightMax, height[right]);
}
}
return ans;
};
Typescript
function trap(height: number[]): number {
if (height.length === 0) {
return 0;
}
let n = height.length;
let left = 0, right = n - 1;
let leftMax = height[left], rightMax = height[right];
let ans = 0;
while (left < right) {
if (leftMax < rightMax) {
ans += leftMax - height[left];
left++;
leftMax = Math.max(leftMax, height[left]);
} else {
ans += rightMax - height[right];
right--;
rightMax = Math.max(rightMax, height[right]);
}
}
return ans;
}
PHP
class Solution {
/**
* @param Integer[] $height
* @return Integer
*/
function trap($height) {
if (count($height) === 0) {
return 0;
}
$n = count($height);
$left = 0;
$right = $n - 1;
$leftMax = $height[$left];
$rightMax = $height[$right];
$ans = 0;
while ($left < $right) {
if ($leftMax < $rightMax) {
$ans += $leftMax - $height[$left];
$left++;
$leftMax = max($leftMax, $height[$left]);
} else {
$ans += $rightMax - $height[$right];
$right--;
$rightMax = max($rightMax, $height[$right]);
}
}
return $ans;
}
}
I hope this helps! Let me know if you have any questions. Don’t forget to follow and give some like to support my content