## Saturday, July 9, 2016

### Volume of Histogram

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.

/* Go through each bar and compute the volume of water above it.
* Volume of water at a bar =
*   height - min(tallest bar on left, tallest bar on right)
*   [where above equation is positive]
* Compute the left max in the first sweep, then sweep again to
* compute the right max, minimum of the bar heights, and the
* delta. */
public static int computeHistogramVolume(int[] histo) {
/* Get left max */
int[] leftMaxes = new int[histo.length];
int leftMax = histo[0];
for (int i = 0; i < histo.length; i++) {
leftMax = Math.max(leftMax, histo[i]);
leftMaxes[i] = leftMax;
}
int sum = 0;
/* Get right max */
int rightMax = histo[histo.length - 1];
for (int i = histo.length - 1; i >= 0; i--) {
rightMax = Math.max(rightMax, histo[i]);
int secondTallest = Math.min(rightMax, leftMaxes[i]);
/* If there are taller things on the left and right side, then there is
* water above this bar. Compute the volume and add to the sum. */
if (secondTallest > histo[i]) {
sum += secondTallest - histo[i];
}
}
return sum;
}

O(N)
public static int borderedVolume(HistogramData[] data, int start, int end) {
if (start >= end) return 0;
int min = Math.min(data[start].getHeight(), data[end].getHeight());
int sum = 0;
for (int i = start + 1; i < end; i++) {
sum += min - data[i].getHeight();
}
return sum;
}

public static int subgraphVolume(HistogramData[] histogram, int start, int end, boolean isLeft) {
if (start >= end) return 0;
int sum = 0;
if (isLeft) {
int max = histogram[end - 1].getLeftMaxIndex();
sum += borderedVolume(histogram, max, end);
sum += subgraphVolume(histogram, start, max, isLeft);
} else {
int max = histogram[start + 1].getRightMaxIndex();
sum += borderedVolume(histogram, start, max);
sum += subgraphVolume(histogram, max, end, isLeft);
}
return sum;
}

public static HistogramData[] createHistogramData(int[] histo) {
HistogramData[] histogram = new HistogramData[histo.length];
for (int i = 0; i < histo.length; i++) {
histogram[i] = new HistogramData(histo[i]);
}
/* Set left max index. */
int maxIndex = 0;
for (int i = 0; i < histo.length; i++) {
if (histo[maxIndex] < histo[i]) {
maxIndex = i;
}
histogram[i].setLeftMaxIndex(maxIndex);
}
/* Set right max index. */
maxIndex = histogram.length - 1;
for (int i = histogram.length - 1; i >= 0; i--) {
if (histo[maxIndex] < histo[i]) {
maxIndex = i;
}
histogram[i].setRightMaxIndex(maxIndex);
}
return histogram;
}

public static int computeHistogramVolume(int[] histogram) {
int start = 0;
int end = histogram.length - 1;
HistogramData[] data = createHistogramData(histogram);
int max = data[0].getRightMaxIndex();
int leftVolume =  subgraphVolume(data, start, max, true);
int rightVolume = subgraphVolume(data, max, end, false);
return leftVolume + rightVolume;
}

O(N^2)
public class HistogramData {

private int height;
private int leftMaxIndex = -1;
private int rightMaxIndex = -1;
public HistogramData(int v) {
height = v;
}
}
public static int findIndexOfMax(int[] histogram, int start, int end) {
int indexOfMax = start;
for (int i = start + 1; i <= end; i++) {
if (histogram[i] > histogram[indexOfMax]) {
indexOfMax = i;
}
}
return indexOfMax;
}

public static int borderedVolume(int[] histogram, int start, int end) {
if (start >= end) return 0;
int min = Math.min(histogram[start], histogram[end]);
int sum = 0;
for (int i = start + 1; i < end; i++) {
sum += min - histogram[i];
}
return sum;
}

public static int subgraphVolume(int[] histogram, int start, int end, boolean isLeft) {
if (start >= end) return 0;
int sum = 0;
if (isLeft) {
int max = findIndexOfMax(histogram, start, end - 1);
sum += borderedVolume(histogram, max, end);
sum += subgraphVolume(histogram, start, max, isLeft);
} else {
int max = findIndexOfMax(histogram, start + 1, end);
sum += borderedVolume(histogram, start, max);
sum += subgraphVolume(histogram, max, end, isLeft);
return sum;
}

public static int computeHistogramVolume(int[] histogram) {
int start = 0;
int end = histogram.length - 1;
int max = findIndexOfMax(histogram, start, end);
int leftVolume =  subgraphVolume(histogram, start, max, true);
int rightVolume = subgraphVolume(histogram, max, end, false);
return leftVolume + rightVolume;
}