Skip to content

Commit d6b091e

Browse files
fix: use fixed-width integers instead of unsigned int
1 parent 05f5062 commit d6b091e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

dynamic_programming/trapped_rainwater.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cassert> /// For assert
1313
#include <cstddef> /// For std::size_t
1414
#include <vector> /// For std::vector
15+
#include <cstdint> /// For integral typedefs
1516

1617
/*
1718
* @namespace
@@ -23,12 +24,12 @@ namespace dynamic_programming {
2324
* @param heights Array representing the heights of walls
2425
* @return The amount of trapped rainwater
2526
*/
26-
unsigned int trappedRainwater(const std::vector<unsigned int>& heights) {
27+
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
2728
std::size_t n = heights.size();
2829
if (n <= 2)
2930
return 0; // No water can be trapped with less than 3 walls
3031

31-
std::vector<unsigned int> leftMax(n), rightMax(n);
32+
std::vector<uint32_t> leftMax(n), rightMax(n);
3233

3334
// Calculate the maximum height of wall to the left of each wall
3435
leftMax[0] = heights[0];
@@ -43,7 +44,7 @@ unsigned int trappedRainwater(const std::vector<unsigned int>& heights) {
4344
}
4445

4546
// Calculate the trapped rainwater between walls
46-
unsigned int trappedWater = 0;
47+
uint32_t trappedWater = 0;
4748
for (std::size_t i = 0; i < n; ++i) {
4849
trappedWater +=
4950
std::max(0u, std::min(leftMax[i], rightMax[i]) - heights[i]);
@@ -59,35 +60,35 @@ unsigned int trappedRainwater(const std::vector<unsigned int>& heights) {
5960
* @returns void
6061
*/
6162
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};
6364
assert(dynamic_programming::trappedRainwater(test_basic) == 6);
6465

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};
6667
assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7);
6768

68-
std::vector<unsigned int> test_bucket = {5, 1, 5};
69+
std::vector<uint32_t> test_bucket = {5, 1, 5};
6970
assert(dynamic_programming::trappedRainwater(test_bucket) == 4);
7071

71-
std::vector<unsigned int> test_skewed_bucket = {4, 1, 5};
72+
std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
7273
assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3);
7374

74-
std::vector<unsigned int> test_empty = {};
75+
std::vector<uint32_t> test_empty = {};
7576
assert(dynamic_programming::trappedRainwater(test_empty) == 0);
7677

77-
std::vector<unsigned int> test_flat = {0, 0, 0, 0, 0};
78+
std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
7879
assert(dynamic_programming::trappedRainwater(test_flat) == 0);
7980

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};
8182
assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0);
8283

83-
std::vector<unsigned int> test_single_elevation = {5};
84+
std::vector<uint32_t> test_single_elevation = {5};
8485
assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0);
8586

86-
std::vector<unsigned int> test_two_point_elevation = {5, 1};
87+
std::vector<uint32_t> test_two_point_elevation = {5, 1};
8788
assert(dynamic_programming::trappedRainwater(test_two_point_elevation) ==
8889
0);
8990

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,
9192
7, 1, 8};
9293
assert(dynamic_programming::trappedRainwater(
9394
test_large_elevation_map_difference) == 15);

0 commit comments

Comments
 (0)