标签:style blog http color java strong 数据 for
题目:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
题解:
一道动态规划的经典题目。需要自底向上求解。
递推公式是: dp[i][j] = dp[i+1][j] + dp[i+1][j+1] ,当前这个点的最小值,由他下面那一行临近的2个点的最小值与当前点的值相加得到。
由于是三角形,且历史数据只在计算最小值时应用一次,所以无需建立二维数组,每次更新1维数组值,最后那个值里存的就是最终结果。
代码如下:
Reference: http://www.programcreek.com/2013/01/leetcode-triangle-java/
Triangle leetcode java,布布扣,bubuko.com
标签:style blog http color java strong 数据 for
原文地址:http://www.cnblogs.com/springfor/p/3887908.html