Repository: TheAlgorithms/C-Plus-Plus
File: dynamic_programming/trapped_rainwater2.cpp (C++)

Code before:
/**
 * @file
 * @brief Implementation of the [Trapped Rainwater
 * Problem](https://www.geeksforgeeks.org/trapping-rain-water/)
 * @details
 * 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](https://github.com/kanavgoyal898)
 */

#include <algorithm>  /// For std::min and std::max
#include <cassert>    /// For assert
#include <cstddef>    /// For std::size_t
#include <cstdint>
#include <vector>  /// For std::vector

/*
 * @namespace
 * @brief Dynamic Programming Algorithms
 */
namespace dynamic_programming {
/**
 * @brief Function to calculate the trapped rainwater
 * @param heights Array representing the heights of walls
 * @return The amount of trapped rainwater
 */
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
    std::size_t n = heights.size();
    if (n <= 2)
        return 0;  // Not enough walls to trap water

    std::size_t left = 0, right = n - 1;
    uint32_t leftMax = 0, rightMax = 0, trappedWater = 0;

    // Traverse from both ends towards the center
    while (left < right) {
        if (heights[left] < heights[right]) {
            // Water trapped depends on the tallest wall to the left
            if (heights[left] >= leftMax)
                leftMax = heights[left];  // Update left max

Review comment (suggestion):
```suggestion
#include <cassert>     /// For assert
#include <cstddef>    /// For std::size_t
#include <cstdint>     /// For unitN_t
#include <vector>      /// For std::vector
```


Code after:
/**
 * @file
 * @brief Implementation of the [Trapped Rainwater
 * Problem](https://www.geeksforgeeks.org/trapping-rain-water/)
 * @details
 * 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](https://github.com/kanavgoyal898)
 */

#include <algorithm>  /// For std::min and std::max
#include <cassert>    /// For assert
#include <cstddef>    /// For std::size_t
#include <cstdint>    /// For std::uint32_t
#include <vector>     /// For std::vector

/*
 * @namespace
 * @brief Dynamic Programming Algorithms
 */
namespace dynamic_programming {
/**
 * @brief Function to calculate the trapped rainwater
 * @param heights Array representing the heights of walls
 * @return The amount of trapped rainwater
 */
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
    std::size_t n = heights.size();
    if (n <= 2)
        return 0;  // Not enough walls to trap water

    std::size_t left = 0, right = n - 1;
    uint32_t leftMax = 0, rightMax = 0, trappedWater = 0;

    // Traverse from both ends towards the center
    while (left < right) {
        if (heights[left] < heights[right]) {
            // Water trapped depends on the tallest wall to the left
            if (heights[left] >= leftMax)
                leftMax = heights[left];  // Update left max