Skip to content

Instantly share code, notes, and snippets.

@AndroidHumanoid
Last active December 27, 2015 00:59
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 AndroidHumanoid/7241202 to your computer and use it in GitHub Desktop.
Save AndroidHumanoid/7241202 to your computer and use it in GitHub Desktop.
Twitter puzzle by Michael Kozakov
http://qandwhat.apps.runkite.com/i-failed-a-twitter-interview/
https://gist.github.com/mkozakov/59af0fd5bddbed1a0399
My solution:
public class Ideone {
public static void main(String[] args) {
int[] myIntArray = {2, 5, 1, 3, 1, 2, 1, 7, 7, 6};
// Author's call
System.out.println(calculateVolume(myIntArray));
// My call
System.out.println(mycalculateVolume(myIntArray));
}
// Author's solution
public static int calculateVolume(int[] land) {
int leftMax = 0;
int rightMax = 0;
int left = 0;
int right = land.length - 1;
int volume = 0;
while(left < right) {
if(land[left] > leftMax) {
leftMax = land[left];
}
if(land[right] > rightMax) {
rightMax = land[right];
}
if(leftMax >= rightMax) {
volume += rightMax - land[right];
right--;
} else {
volume += leftMax - land[left];
left++;
}
}
return volume;
}
// My solution (but unfortunately it doesn't work for [2, 5, 1, 2, 3, 4, 7, 5, 6] which should give 11):
public static int mycalculateVolume(int[] land) {
int leftMax = 0;
int subvolume = 0;
int volume = 0;
for (int index = 0; index < land.length; index++) {
if (land[index] >= leftMax){
leftMax = land[index];
volume = volume + subvolume;
subvolume = 0;
}
else {
subvolume = subvolume + leftMax - land[index];
}
}
return volume;
}
// My solution, plan B in progress:
http://it.laplit.com/index.php/humanoid-blog/128-article-7
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment