TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
trapped_rainwater2.cpp
Go to the documentation of this file.
1
13
14#include <algorithm>
15#include <cassert>
16#include <cstddef>
17#include <cstdint>
18#include <vector>
19
20/*
21 * @namespace
22 * @brief Dynamic Programming Algorithms
23 */
24namespace dynamic_programming {
30uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
31 std::size_t n = heights.size();
32 if (n <= 2)
33 return 0; // Not enough walls to trap water
34
35 std::size_t left = 0, right = n - 1;
36 uint32_t leftMax = 0, rightMax = 0, trappedWater = 0;
37
38 // Traverse from both ends towards the center
39 while (left < right) {
40 if (heights[left] < heights[right]) {
41 // Water trapped depends on the tallest wall to the left
42 if (heights[left] >= leftMax)
43 leftMax = heights[left]; // Update left max
44 else
45 trappedWater +=
46 leftMax - heights[left]; // Water trapped at current left
47 ++left;
48 } else {
49 // Water trapped depends on the tallest wall to the right
50 if (heights[right] >= rightMax)
51 rightMax = heights[right]; // Update right max
52 else
53 trappedWater +=
54 rightMax -
55 heights[right]; // Water trapped at current right
56 --right;
57 }
58 }
59
60 return trappedWater;
61}
62
63} // namespace dynamic_programming
64
69static void test() {
70 std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
71 assert(dynamic_programming::trappedRainwater(test_basic) == 6);
72
73 std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4};
74 assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7);
75
76 std::vector<uint32_t> test_bucket = {5, 1, 5};
77 assert(dynamic_programming::trappedRainwater(test_bucket) == 4);
78
79 std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
80 assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3);
81
82 std::vector<uint32_t> test_empty = {};
83 assert(dynamic_programming::trappedRainwater(test_empty) == 0);
84
85 std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
86 assert(dynamic_programming::trappedRainwater(test_flat) == 0);
87
88 std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0};
89 assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0);
90
91 std::vector<uint32_t> test_single_elevation = {5};
92 assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0);
93
94 std::vector<uint32_t> test_two_point_elevation = {5, 1};
95 assert(dynamic_programming::trappedRainwater(test_two_point_elevation) ==
96 0);
97
98 std::vector<uint32_t> test_large_elevation_map_difference = {5, 1, 6, 1,
99 7, 1, 8};
101 test_large_elevation_map_difference) == 15);
102}
103
108int main() {
109 test(); // run self-test implementations
110 return 0;
111}
Dynamic Programming algorithms.
uint32_t trappedRainwater(const std::vector< uint32_t > &heights)
Function to calculate the trapped rainwater.
static void test()
Self-test implementations.
int main()
Main function.