Skip to content

Instantly share code, notes, and snippets.

@bluerogue
Created April 5, 2015 18:00
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 bluerogue/2ad2d587b42b340e79f1 to your computer and use it in GitHub Desktop.
Save bluerogue/2ad2d587b42b340e79f1 to your computer and use it in GitHub Desktop.
Largest sum from adjacent path
Map<Integer, Integer[]> values = populate(); // should typically be a 2D array of type 'int' instead
for (int i = values.size() - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
values.get(i)[j] +=
Math.max(values.get(i + 1)[j], values.get(i + 1)[j + 1]);
}
}
int largestPathSum = values.get(0)[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment