![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of the Trapped Rainwater Problem More...
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>
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. |
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.
Definition in file trapped_rainwater2.cpp.
int main | ( | void | ) |
Main function.
Definition at line 108 of file trapped_rainwater2.cpp.
|
static |
Self-test implementations.
Definition at line 69 of file trapped_rainwater2.cpp.