Created
December 3, 2015 03:52
-
-
Save junminstorage/ee07e8e6e260cc1ca2ed to your computer and use it in GitHub Desktop.
min path sum for number triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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