1. Prefix & Suffix Max Arrays
Precompute the tallest bar to the left and right of every index in two passes, then sum min(leftMax[i], rightMax[i]) - height[i].
Three linear passes over the n elevation bars.
Stores leftMax and rightMax arrays of size n.
How It Works
Deriving the Bottleneck Directly
Above every index i, water rises up to the shorter of the tallest wall on its left and tallest wall on its right:
water[i] = min(leftMax[i], rightMax[i]) - height[i]
Precomputing those boundaries explicitly takes three clean passes:
- Forward scan builds
leftMax[i] = max(leftMax[i - 1], height[i]). - Backward scan builds
rightMax[i] = max(rightMax[i + 1], height[i]). - Final pass accumulates
min(leftMax[i], rightMax[i]) - height[i].
Step-by-Step Dry Run
For [4, 2, 0, 3, 2, 5], running left maxima become [4, 4, 4, 4, 4, 5].
leftMax = [4, 4, 4, 4, 4, 5]- +Direct translation of the water-level formula
- +Easy to verify and debug
- -Allocates O(n) extra arrays where O(1) space is achievable
Reference Implementation
Complete implementation for Prefix & Suffix Max Arrays in Python, JavaScript, Java, C++, and C.
def trap_arrays(height: list[int]) -> int:n = len(height)if n == 0:return 0left_max = [0] * nright_max = [0] * nleft_max[0] = height[0]for i in range(1, n):left_max[i] = max(left_max[i - 1], height[i])right_max[n - 1] = height[n - 1]for i in range(n - 2, -1, -1):right_max[i] = max(right_max[i + 1], height[i])return sum(min(left_max[i], right_max[i]) - height[i] for i in range(n))if __name__ == "__main__":print("Trapped:", trap_arrays([4, 2, 0, 3, 2, 5]))
function trapArrays(height) {
const n = height.length;
if (n === 0) return 0;
const leftMax = new Array(n);
const rightMax = new Array(n);
leftMax[0] = height[0];
for (let i = 1; i < n; i++) leftMax[i] = Math.max(leftMax[i - 1], height[i]);
rightMax[n - 1] = height[n - 1];
for (let i = n - 2; i >= 0; i--) rightMax[i] = Math.max(rightMax[i + 1], height[i]);
let water = 0;
for (let i = 0; i < n; i++) {
water += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return water;
}
console.log("Trapped:", trapArrays([4, 2, 0, 3, 2, 5]));public class Solution {
public static int trapArrays(int[] height) {
int n = height.length;
if (n == 0) return 0;
int[] leftMax = new int[n];
int[] rightMax = new int[n];
leftMax[0] = height[0];
for (int i = 1; i < n; i++) leftMax[i] = Math.max(leftMax[i - 1], height[i]);
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) rightMax[i] = Math.max(rightMax[i + 1], height[i]);
int water = 0;
for (int i = 0; i < n; i++) {
water += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return water;
}
}#include <vector>
#include <algorithm>
int trapArrays(const std::vector<int>& height) {
int n = static_cast<int>(height.size());
if (n == 0) return 0;
std::vector<int> leftMax(n), rightMax(n);
leftMax[0] = height[0];
for (int i = 1; i < n; i++) leftMax[i] = std::max(leftMax[i - 1], height[i]);
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) rightMax[i] = std::max(rightMax[i + 1], height[i]);
int water = 0;
for (int i = 0; i < n; i++) {
water += std::min(leftMax[i], rightMax[i]) - height[i];
}
return water;
}#include <stdio.h>
#include <stdlib.h>
int trapArrays(const int* height, int n) {
if (n == 0) return 0;
int* leftMax = (int*)malloc(n * sizeof(int));
int* rightMax = (int*)malloc(n * sizeof(int));
leftMax[0] = height[0];
for (int i = 1; i < n; i++) {
leftMax[i] = height[i] > leftMax[i - 1] ? height[i] : leftMax[i - 1];
}
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) {
rightMax[i] = height[i] > rightMax[i + 1] ? height[i] : rightMax[i + 1];
}
int water = 0;
for (int i = 0; i < n; i++) {
int wall = leftMax[i] < rightMax[i] ? leftMax[i] : rightMax[i];
water += wall - height[i];
}
free(leftMax);
free(rightMax);
return water;
}