Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created October 30, 2013 14:45
Show Gist options
  • Save ylegall/7233866 to your computer and use it in GitHub Desktop.
Save ylegall/7233866 to your computer and use it in GitHub Desktop.
how much liquid will accumulate between the stacked boxes.
#include <stdio.h>
#include <stdlib.h>
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
void skyscrapers(int *h, int len) {
int* pks = (int*)malloc(sizeof(int)*len);
int i, p=h[0], v=0, sum=0;
v = pks[0] = h[0];
for (i = 1; i < len; ++i) {
if (h[i] > v) v = h[i];
pks[i] = v;
}
v = pks[len-1] = h[len-1];
for (i = len-2; i >= 0; --i) {
if (h[i] > v) v = h[i];
pks[i] = MIN(pks[i], v);
}
for (i = 0; i < len; ++i) sum += pks[i] - h[i];
printf("%d", sum);
free(pks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment