TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
trapped_rainwater2.cpp File Reference

Implementation of the Trapped Rainwater Problem More...

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>
Include dependency graph for trapped_rainwater2.cpp:

Go to the source code of this file.

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.

Functions

uint32_t dynamic_programming::trappedRainwater (const std::vector< uint32_t > &heights)
 Function to calculate the trapped rainwater.
static void test ()
 Self-test implementations.
int main ()
 Main function.

Detailed Description

Implementation of the Trapped Rainwater Problem

This implementation calculates the total trapped rainwater using a two-pointer approach. It maintains two pointers (left and right) and tracks the maximum height seen so far from both ends (leftMax and rightMax). At each step, the algorithm decides which side to process based on which boundary is smaller, ensuring O(n) time and O(1) space complexity.

Author
kanavgoyal898

Definition in file trapped_rainwater2.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 108 of file trapped_rainwater2.cpp.

108 {
109 test(); // run self-test implementations
110 return 0;
111}
static void test()
Self-test implementations.

◆ test()

void test ( )
static

Self-test implementations.

Returns
void

Definition at line 69 of file trapped_rainwater2.cpp.

69 {
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}
uint32_t trappedRainwater(const std::vector< uint32_t > &heights)
Function to calculate the trapped rainwater.