Skip to content

Instantly share code, notes, and snippets.

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