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