Skip to content

Instantly share code, notes, and snippets.

@junminstorage
Created December 3, 2015 03:54
Show Gist options
  • Save junminstorage/6e4a88f34dcabda54cdd to your computer and use it in GitHub Desktop.
Save junminstorage/6e4a88f34dcabda54cdd to your computer and use it in GitHub Desktop.
min path for number triangle
public static int minPathBetter(List<int[]> input){
int levels = input.size();
int[] dp = new int[levels];
//we start at the bottom
//here the min path sum is the numbers at the bottom
dp = input.get(levels-1);
//now we go up
for(int l=levels-2; l>=0; l--){
for(int pos = 0; pos<=l; pos++){
dp[pos] = Math.min(dp[pos], dp[pos+1]) + input.get(l)[pos];
}
}
return dp[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment