Skip to content

Instantly share code, notes, and snippets.

/TwitterRain
Created Oct 30, 2013

Embed
What would you like to do?
class Rain
{
public static void main (String[] args)
{
int[] walls = {2, 5, 1, 2, 3, 4, 7, 7, 6};
System.out.println(calculateVolume(walls));
}
public static int calculateVolume(int[] walls) {
int prev = 0;
int max = 0;
int localSum = 0;
int totalSum = 0;
for (int i=0; i<walls.length; i++) {
if (i < prev && max==0) max = prev;
if (walls[i] >= max) {
max = 0;
totalSum += localSum;
localSum = 0;
}
if (max != 0) {
localSum += max - walls[i];
}
prev = walls[i];
}
return totalSum;
}
}
@thisvar

This comment has been minimized.

Copy link

commented Oct 30, 2013

oh, shame ( bad solution, don't watch it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.