>what's the runtime on that? n^2?
I'm hopeless with big O, but I think you're saying my solution is shit. Let's benchmark it.
{ 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4, 0, 3, 2, 1, 2, 1, 4 }
My old code can run this a million times in 1000 ms. With some changes, I've gotten it down to 140 ms:
static long WaterArea(uint[] bar)
{
long water = 0;
for (int i = 0; i < bar.Length - 1;) {
//Find a concave area between two bars
int tallest = i + 1;
for (int j = i + 1; j < bar.Length; j++) {
if (bar[j] == 0) {
tallest = i + 1;
break;
}
if (bar[j] >= bar[tallest]) {
tallest = j;
if (bar[j] >= bar[i]) {
break;
}
}
}
//Fill the area to match the height of the shorter bar
uint waterHeight = Math.Min(bar[i], bar[tallest]);
for (int k = i + 1; k < tallest; k++) {
if (bar[k] < waterHeight) {
water += waterHeight - bar[k];
}
}
//Skip to the next segment
i = tallest;
}
return water;
}
is 250 times slower than my code. Pic related.
Attached: benchmark.png (69x35, 548)