std::size_t n = heights.size();
32std::vector<uint32_t> leftMax(n), rightMax(n);
35leftMax[0] = heights[0];
36 for(std::size_t i = 1; i < n; ++i) {
37leftMax[i] = std::max(leftMax[i - 1], heights[i]);
41rightMax[n - 1] = heights[n - 1];
42 for(std::size_t i = n - 2; i < n; --i) {
43rightMax[i] = std::max(rightMax[i + 1], heights[i]);
47uint32_t trappedWater = 0;
48 for(std::size_t i = 0; i < n; ++i) {
50std::max(0u, std::min(leftMax[i], rightMax[i]) - heights[i]);
63std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
66std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4};
69std::vector<uint32_t> test_bucket = {5, 1, 5};
72std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
75std::vector<uint32_t> test_empty = {};
78std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
81std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0};
84std::vector<uint32_t> test_single_elevation = {5};
87std::vector<uint32_t> test_two_point_elevation = {5, 1};
91std::vector<uint32_t> test_large_elevation_map_difference = {5, 1, 6, 1,
94test_large_elevation_map_difference) == 15);
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4