diff --git a/d4/de3/trapped__rainwater2_8cpp.html b/d4/de3/trapped__rainwater2_8cpp.html new file mode 100644 index 000000000..294a52347 --- /dev/null +++ b/d4/de3/trapped__rainwater2_8cpp.html @@ -0,0 +1,265 @@ + + + + + + + + +TheAlgorithms/C++: dynamic_programming/trapped_rainwater2.cpp File Reference + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
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.
+
+
+
+
+
+ +
+ + + + diff --git a/d4/de3/trapped__rainwater2_8cpp.js b/d4/de3/trapped__rainwater2_8cpp.js new file mode 100644 index 000000000..4b4f03140 --- /dev/null +++ b/d4/de3/trapped__rainwater2_8cpp.js @@ -0,0 +1,6 @@ +var trapped__rainwater2_8cpp = +[ + [ "main", "d4/de3/trapped__rainwater2_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], + [ "test", "d4/de3/trapped__rainwater2_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ], + [ "dynamic_programming::trappedRainwater", "dd/d24/namespacedynamic__programming.html#a066e0e739e7c276eee6e61d5b4d37ce8", null ] +]; \ No newline at end of file diff --git a/d4/de3/trapped__rainwater2_8cpp_source.html b/d4/de3/trapped__rainwater2_8cpp_source.html new file mode 100644 index 000000000..312893ddd --- /dev/null +++ b/d4/de3/trapped__rainwater2_8cpp_source.html @@ -0,0 +1,225 @@ + + + + + + + + +TheAlgorithms/C++: dynamic_programming/trapped_rainwater2.cpp Source File + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
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.
+
+
+
+ + + + diff --git a/d6/d04/trapped__rainwater2_8cpp__incl.map b/d6/d04/trapped__rainwater2_8cpp__incl.map new file mode 100644 index 000000000..6b2c2e3e9 --- /dev/null +++ b/d6/d04/trapped__rainwater2_8cpp__incl.map @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/d6/d04/trapped__rainwater2_8cpp__incl.md5 b/d6/d04/trapped__rainwater2_8cpp__incl.md5 new file mode 100644 index 000000000..224d5c5b7 --- /dev/null +++ b/d6/d04/trapped__rainwater2_8cpp__incl.md5 @@ -0,0 +1 @@ +be0c196d02274ca4fd6978f0dcc6a5f9 \ No newline at end of file diff --git a/d6/d04/trapped__rainwater2_8cpp__incl.svg b/d6/d04/trapped__rainwater2_8cpp__incl.svg new file mode 100644 index 000000000..86a85a04e --- /dev/null +++ b/d6/d04/trapped__rainwater2_8cpp__incl.svg @@ -0,0 +1,138 @@ + + + + + + + + + + + + +dynamic_programming/trapped_rainwater2.cpp + + +Node1 + + +dynamic_programming +/trapped_rainwater2.cpp + + + + + +Node2 + + +algorithm + + + + + +Node1->Node2 + + + + + + + + +Node3 + + +cassert + + + + + +Node1->Node3 + + + + + + + + +Node4 + + +cstddef + + + + + +Node1->Node4 + + + + + + + + +Node5 + + +cstdint + + + + + +Node1->Node5 + + + + + + + + +Node6 + + +vector + + + + + +Node1->Node6 + + + + + + + + + + + + + diff --git a/d6/d04/trapped__rainwater2_8cpp__incl_org.svg b/d6/d04/trapped__rainwater2_8cpp__incl_org.svg new file mode 100644 index 000000000..c5e5204c1 --- /dev/null +++ b/d6/d04/trapped__rainwater2_8cpp__incl_org.svg @@ -0,0 +1,112 @@ + + + + + + +dynamic_programming/trapped_rainwater2.cpp + + +Node1 + + +dynamic_programming +/trapped_rainwater2.cpp + + + + + +Node2 + + +algorithm + + + + + +Node1->Node2 + + + + + + + + +Node3 + + +cassert + + + + + +Node1->Node3 + + + + + + + + +Node4 + + +cstddef + + + + + +Node1->Node4 + + + + + + + + +Node5 + + +cstdint + + + + + +Node1->Node5 + + + + + + + + +Node6 + + +vector + + + + + +Node1->Node6 + + + + + + + + diff --git a/dd/d24/namespacedynamic__programming.html b/dd/d24/namespacedynamic__programming.html index b43090467..36b827d11 100644 --- a/dd/d24/namespacedynamic__programming.html +++ b/dd/d24/namespacedynamic__programming.html @@ -157,7 +157,8 @@ Functions

Dynamic Programming algorithms

for std::assert for IO operations for unordered map

Dynamic Programming algorithms

-

For std::min and std::max For assert For std::size_t

+

For std::min and std::max For assert For std::size_t

+

For std::min and std::max For assert For std::size_t For std::uint32_t For std::vector

Function Documentation

◆ is_armstrong()

diff --git a/dir_8a20dd5bfd5341a725342bf72b6b686f.html b/dir_8a20dd5bfd5341a725342bf72b6b686f.html index 0e874305b..de5fbfdaa 100644 --- a/dir_8a20dd5bfd5341a725342bf72b6b686f.html +++ b/dir_8a20dd5bfd5341a725342bf72b6b686f.html @@ -164,6 +164,8 @@ Files  Implements [Sub-set sum problem] (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells whether a subset with target sum exists or not.
 trapped_rainwater.cpp  Implementation of the Trapped Rainwater Problem
+
 trapped_rainwater2.cpp + Implementation of the Trapped Rainwater Problem
 tree_height.cpp
 unbounded_0_1_knapsack.cpp  Implementation of the Unbounded 0/1 Knapsack Problem.
diff --git a/dir_8a20dd5bfd5341a725342bf72b6b686f.js b/dir_8a20dd5bfd5341a725342bf72b6b686f.js index 0af5548c1..3cc3cb526 100644 --- a/dir_8a20dd5bfd5341a725342bf72b6b686f.js +++ b/dir_8a20dd5bfd5341a725342bf72b6b686f.js @@ -28,6 +28,7 @@ var dir_8a20dd5bfd5341a725342bf72b6b686f = [ "shortest_common_supersequence.cpp", "d7/d65/shortest__common__supersequence_8cpp.html", "d7/d65/shortest__common__supersequence_8cpp" ], [ "subset_sum_dynamic.cpp", "dc/d67/subset__sum__dynamic_8cpp.html", "dc/d67/subset__sum__dynamic_8cpp" ], [ "trapped_rainwater.cpp", "d9/d80/trapped__rainwater_8cpp.html", "d9/d80/trapped__rainwater_8cpp" ], + [ "trapped_rainwater2.cpp", "d4/de3/trapped__rainwater2_8cpp.html", "d4/de3/trapped__rainwater2_8cpp" ], [ "tree_height.cpp", "de/d77/tree__height_8cpp_source.html", null ], [ "unbounded_0_1_knapsack.cpp", "d9/dec/unbounded__0__1__knapsack_8cpp.html", "d9/dec/unbounded__0__1__knapsack_8cpp" ], [ "word_break.cpp", "d3/d84/word__break_8cpp.html", "d3/d84/word__break_8cpp" ] diff --git a/doxygen_crawl.html b/doxygen_crawl.html index 5c40e8442..811164b92 100644 --- a/doxygen_crawl.html +++ b/doxygen_crawl.html @@ -910,6 +910,10 @@ + + + + diff --git a/files.html b/files.html index 587acc536..e7969ecd2 100644 --- a/files.html +++ b/files.html @@ -235,9 +235,10 @@ solve-a-rat-in-a-maze-c-java-pytho/" target="_blank">Rat in a Maze algorithm  
shortest_common_supersequence.cppSCS is a string Z which is the shortest supersequence of strings X and Y (may not be continuous in Z, but order is maintained)  
subset_sum_dynamic.cppImplements [Sub-set sum problem] (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells whether a subset with target sum exists or not  
trapped_rainwater.cppImplementation of the Trapped Rainwater Problem - 
tree_height.cpp - 
unbounded_0_1_knapsack.cppImplementation of the Unbounded 0/1 Knapsack Problem - 
word_break.cppWord Break Problem + 
trapped_rainwater2.cppImplementation of the Trapped Rainwater Problem + 
tree_height.cpp + 
unbounded_0_1_knapsack.cppImplementation of the Unbounded 0/1 Knapsack Problem + 
word_break.cppWord Break Problem  
games  
memory_game.cppA simple Memory Game with 3 different sizes and multiple letters  
geometry diff --git a/globals_func_m.html b/globals_func_m.html index 4561d762d..474e816ff 100644 --- a/globals_func_m.html +++ b/globals_func_m.html @@ -116,7 +116,7 @@ $(function(){initNavTree('globals_func_m.html','',''); });
Here is a list of all documented functions with links to the documentation:

- m -