-
Notifications
You must be signed in to change notification settings - Fork 155
/
problem_17_21_histogramVolume.h
47 lines (41 loc) · 1.6 KB
/
problem_17_21_histogramVolume.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
PROBLEM:
Imagine a histogram (bar graph). Design an algorithm to compute the volume of water
it could hold if someone poured water across the top. You can assume that each histogram
bar has width 1.
ALGORITHM:
heights [0,1,0,2,1,0,1,3,2,1,2,1]
maxFromLeft [0,1,1,2,2,2,2,3,3,3,3,3]
maxFromRight[3,3,3,3,3,3,3,3,2,2,2,1]
We observe that the accumulated water at position X is equal to min(max_to_left, max_to_right) - height(X)
TIME COMPLEXITY: O(N)
SPACE COMPLEXITY: O(N)
*/
#pragma once
#include <algorithm>
#include <vector>
namespace chapter_17 {
template <typename T>
T histogramVolume(const std::vector<T>& histogram) {
std::vector<int> maxHeightsToLeft(histogram.size(), 0);
std::vector<int> maxHeightsToRight(histogram.size(), 0);
int maxHeightToLeft = 0;
int maxHeightToRight = 0;
int totalAccumulation = 0;
// compute maxFromLeft for each position
for (int i = 0; i < histogram.size(); i++) {
if (histogram[i] > maxHeightToLeft) maxHeightToLeft = histogram[i];
maxHeightsToLeft[i] = maxHeightToLeft;
}
// compute maxFromRight for each position
for (int i = histogram.size() - 1; i >= 0; i--) {
if (histogram[i] > maxHeightToRight) maxHeightToRight = histogram[i];
maxHeightsToRight[i] = maxHeightToRight;
}
// compute water accumulation at each position
for (int i = 0; i < histogram.size(); i++) {
totalAccumulation += (std::min(maxHeightsToLeft[i], maxHeightsToRight[i]) - histogram[i]);
}
return totalAccumulation;
}
}