Skip to content

Commit 05f5062

Browse files
committed
ref: use unsigned int for height of walls
1 parent 5be0b58 commit 05f5062

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

dynamic_programming/trapped_rainwater.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <algorithm> /// For std::min and std::max
1212
#include <cassert> /// For assert
13+
#include <cstddef> /// For std::size_t
1314
#include <vector> /// For std::vector
1415

1516
/*
@@ -22,30 +23,30 @@ namespace dynamic_programming {
2223
* @param heights Array representing the heights of walls
2324
* @return The amount of trapped rainwater
2425
*/
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();
2728
if (n <= 2)
2829
return 0; // No water can be trapped with less than 3 walls
2930

30-
std::vector<int> leftMax(n), rightMax(n);
31+
std::vector<unsigned int> leftMax(n), rightMax(n);
3132

3233
// Calculate the maximum height of wall to the left of each wall
3334
leftMax[0] = heights[0];
34-
for (int i = 1; i < n; ++i) {
35+
for (std::size_t i = 1; i < n; ++i) {
3536
leftMax[i] = std::max(leftMax[i - 1], heights[i]);
3637
}
3738

3839
// Calculate the maximum height of wall to the right of each wall
3940
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) {
4142
rightMax[i] = std::max(rightMax[i + 1], heights[i]);
4243
}
4344

4445
// 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) {
4748
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]);
4950
}
5051

5152
return trappedWater;
@@ -58,36 +59,36 @@ int trappedRainwater(const std::vector<int>& heights) {
5859
* @returns void
5960
*/
6061
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};
6263
assert(dynamic_programming::trappedRainwater(test_basic) == 6);
6364

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)