28 std::size_t n = heights.size();
32 std::vector<uint32_t> leftMax(n), rightMax(n);
35 leftMax[0] = heights[0];
36 for (std::size_t i = 1; i < n; ++i) {
37 leftMax[i] = std::max(leftMax[i - 1], heights[i]);
41 rightMax[n - 1] = heights[n - 1];
42 for (std::size_t i = n - 2; i < n; --i) {
43 rightMax[i] = std::max(rightMax[i + 1], heights[i]);
47 uint32_t trappedWater = 0;
48 for (std::size_t i = 0; i < n; ++i) {
50 std::max(0u, std::min(leftMax[i], rightMax[i]) - heights[i]);
63 std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
66 std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4};
69 std::vector<uint32_t> test_bucket = {5, 1, 5};
72 std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
75 std::vector<uint32_t> test_empty = {};
78 std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
81 std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0};
84 std::vector<uint32_t> test_single_elevation = {5};
87 std::vector<uint32_t> test_two_point_elevation = {5, 1};
91 std::vector<uint32_t> test_large_elevation_map_difference = {5, 1, 6, 1,
94 test_large_elevation_map_difference) == 15);