42. Trapping Rain Water
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to calculate the total amount of water that can be trapped by an elevation map represented by an array of non-negative integers. The amount of water trapped at any given position is determined by the walls around it. Specifically, for a bar at index i, the water level can rise up to the height of the lower of the two highest bars to its left and right. The amount of water trapped above bar i is this water level minus the height of the bar at i.
Solution
Trapping Rain Water in 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 = 0, 0
total_water = 0
# Process the array from both ends using two pointers
while left < right:
# If the left wall is shorter than the right wall
if height[left] < height[right]:
# The amount of water trapped is determined by the left_max wall
if height[left] >= left_max:
# This bar is a new highest wall from the left, so it can't trap water itself.
# Update the max height seen from the left.
left_max = height[left]
else:
# This bar is lower than the max height from the left, so it traps water.
# The amount of water is the difference between the left_max and its own height.
total_water += left_max - height[left]
# Move the left pointer inwards.
left += 1
else: # height[left] >= height[right]
# The right wall is shorter or equal, so it's the bottleneck.
if height[right] >= right_max:
# This bar is a new highest wall from the right.
# Update the max height seen from the right.
right_max = height[right]
else:
# This bar is lower than the max height from the right, so it traps water.
total_water += right_max - height[right]
# Move the right pointer inwards.
right -= 1
return total_water
Trapping Rain Water in Java
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int n = height.length;
int left = 0, right = n - 1;
int leftMax = 0, rightMax = 0;
int totalWater = 0;
// Process the array from both ends using two pointers
while (left < right) {
// If the left wall is shorter than the right wall
if (height[left] < height[right]) {
// The amount of water trapped is determined by the leftMax wall
if (height[left] >= leftMax) {
// This bar is a new highest wall from the left, so it can't trap water itself.
// Update the max height seen from the left.
leftMax = height[left];
} else {
// This bar is lower than the max height from the left, so it traps water.
// The amount of water is the difference between leftMax and its own height.
totalWater += leftMax - height[left];
}
// Move the left pointer inwards.
left++;
} else { // height[left] >= height[right]
// The right wall is shorter or equal, so it's the bottleneck.
if (height[right] >= rightMax) {
// This bar is a new highest wall from the right.
// Update the max height seen from the right.
rightMax = height[right];
} else {
// This bar is lower than the max height from the right, so it traps water.
totalWater += rightMax - height[right];
}
// Move the right pointer inwards.
right--;
}
}
return totalWater;
}
}
Trapping Rain Water in C++
class Solution {
public:
int trap(vector<int>& height) {
if (height.empty()) {
return 0;
}
int n = height.size();
int left = 0, right = n - 1;
int left_max = 0, right_max = 0;
int total_water = 0;
// Process the array from both ends using two pointers
while (left < right) {
// If the left wall is shorter than the right wall
if (height[left] < height[right]) {
// The amount of water trapped is determined by the left_max wall
if (height[left] >= left_max) {
// This bar is a new highest wall from the left, so it can't trap water itself.
// Update the max height seen from the left.
left_max = height[left];
} else {
// This bar is lower than the max height from the left, so it traps water.
// The amount of water is the difference between left_max and its own height.
total_water += left_max - height[left];
}
// Move the left pointer inwards.
left++;
} else { // height[left] >= height[right]
// The right wall is shorter or equal, so it's the bottleneck.
if (height[right] >= right_max) {
// This bar is a new highest wall from the right.
// Update the max height seen from the right.
right_max = height[right];
} else {
// This bar is lower than the max height from the right, so it traps water.
total_water += right_max - height[right];
}
// Move the right pointer inwards.
right--;
}
}
return total_water;
}
};
Trapping Rain Water in TypeScript
function trap(height: number[]): number {
if (!height || height.length === 0) {
return 0;
}
const n = height.length;
let left = 0, right = n - 1;
let leftMax = 0, rightMax = 0;
let totalWater = 0;
// Process the array from both ends using two pointers
while (left < right) {
// If the left wall is shorter than the right wall
if (height[left] < height[right]) {
// The amount of water trapped is determined by the leftMax wall
if (height[left] >= leftMax) {
// This bar is a new highest wall from the left, so it can't trap water itself.
// Update the max height seen from the left.
leftMax = height[left];
} else {
// This bar is lower than the max height from the left, so it traps water.
// The amount of water is the difference between leftMax and its own height.
totalWater += leftMax - height[left];
}
// Move the left pointer inwards.
left++;
} else { // height[left] >= height[right]
// The right wall is shorter or equal, so it's the bottleneck.
if (height[right] >= rightMax) {
// This bar is a new highest wall from the right.
// Update the max height seen from the right.
rightMax = height[right];
} else {
// This bar is lower than the max height from the right, so it traps water.
totalWater += rightMax - height[right];
}
// Move the right pointer inwards.
right--;
}
}
return totalWater;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.