Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2013 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7236132 to your computer and use it in GitHub Desktop.
Save anonymous/7236132 to your computer and use it in GitHub Desktop.
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
Copy link

thisvar commented Oct 30, 2013

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

@russdreamer
Copy link

Not bad. The only disadvantage - it does not work for 99% cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment