12
12
#include < cassert> // / For assert
13
13
#include < cstddef> // / For std::size_t
14
14
#include < vector> // / For std::vector
15
+ #include < cstdint> // / For integral typedefs
15
16
16
17
/*
17
18
* @namespace
@@ -23,12 +24,12 @@ namespace dynamic_programming {
23
24
* @param heights Array representing the heights of walls
24
25
* @return The amount of trapped rainwater
25
26
*/
26
- unsigned int trappedRainwater (const std::vector<unsigned int >& heights) {
27
+ uint32_t trappedRainwater (const std::vector<uint32_t >& heights) {
27
28
std::size_t n = heights.size ();
28
29
if (n <= 2 )
29
30
return 0 ; // No water can be trapped with less than 3 walls
30
31
31
- std::vector<unsigned int > leftMax (n), rightMax (n);
32
+ std::vector<uint32_t > leftMax (n), rightMax (n);
32
33
33
34
// Calculate the maximum height of wall to the left of each wall
34
35
leftMax[0 ] = heights[0 ];
@@ -43,7 +44,7 @@ unsigned int trappedRainwater(const std::vector<unsigned int>& heights) {
43
44
}
44
45
45
46
// Calculate the trapped rainwater between walls
46
- unsigned int trappedWater = 0 ;
47
+ uint32_t trappedWater = 0 ;
47
48
for (std::size_t i = 0 ; i < n; ++i) {
48
49
trappedWater +=
49
50
std::max (0u , std::min (leftMax[i], rightMax[i]) - heights[i]);
@@ -59,35 +60,35 @@ unsigned int trappedRainwater(const std::vector<unsigned int>& heights) {
59
60
* @returns void
60
61
*/
61
62
static void test () {
62
- std::vector<unsigned int > test_basic = {0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 };
63
+ std::vector<uint32_t > test_basic = {0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 };
63
64
assert (dynamic_programming::trappedRainwater (test_basic) == 6 );
64
65
65
- std::vector<unsigned int > test_peak_under_water = {3 , 0 , 2 , 0 , 4 };
66
+ std::vector<uint32_t > test_peak_under_water = {3 , 0 , 2 , 0 , 4 };
66
67
assert (dynamic_programming::trappedRainwater (test_peak_under_water) == 7 );
67
68
68
- std::vector<unsigned int > test_bucket = {5 , 1 , 5 };
69
+ std::vector<uint32_t > test_bucket = {5 , 1 , 5 };
69
70
assert (dynamic_programming::trappedRainwater (test_bucket) == 4 );
70
71
71
- std::vector<unsigned int > test_skewed_bucket = {4 , 1 , 5 };
72
+ std::vector<uint32_t > test_skewed_bucket = {4 , 1 , 5 };
72
73
assert (dynamic_programming::trappedRainwater (test_skewed_bucket) == 3 );
73
74
74
- std::vector<unsigned int > test_empty = {};
75
+ std::vector<uint32_t > test_empty = {};
75
76
assert (dynamic_programming::trappedRainwater (test_empty) == 0 );
76
77
77
- std::vector<unsigned int > test_flat = {0 , 0 , 0 , 0 , 0 };
78
+ std::vector<uint32_t > test_flat = {0 , 0 , 0 , 0 , 0 };
78
79
assert (dynamic_programming::trappedRainwater (test_flat) == 0 );
79
80
80
- std::vector<unsigned int > test_no_trapped_water = {1 , 1 , 2 , 4 , 0 , 0 , 0 };
81
+ std::vector<uint32_t > test_no_trapped_water = {1 , 1 , 2 , 4 , 0 , 0 , 0 };
81
82
assert (dynamic_programming::trappedRainwater (test_no_trapped_water) == 0 );
82
83
83
- std::vector<unsigned int > test_single_elevation = {5 };
84
+ std::vector<uint32_t > test_single_elevation = {5 };
84
85
assert (dynamic_programming::trappedRainwater (test_single_elevation) == 0 );
85
86
86
- std::vector<unsigned int > test_two_point_elevation = {5 , 1 };
87
+ std::vector<uint32_t > test_two_point_elevation = {5 , 1 };
87
88
assert (dynamic_programming::trappedRainwater (test_two_point_elevation) ==
88
89
0 );
89
90
90
- std::vector<unsigned int > test_large_elevation_map_difference = {5 , 1 , 6 , 1 ,
91
+ std::vector<uint32_t > test_large_elevation_map_difference = {5 , 1 , 6 , 1 ,
91
92
7 , 1 , 8 };
92
93
assert (dynamic_programming::trappedRainwater (
93
94
test_large_elevation_map_difference) == 15 );
0 commit comments