Description
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
1 | For example, given the following triangle |
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.
Method
这是一个动态规划问题,用一个数组存入每一个数字的最优值,然后求最后一行的最小值即可。
具体流程如下:
- 初始
dp[0][0] = triangle.get(0).get(0)
。 dp[i][j]
会等于相邻父亲数字里最小值加上他本身的值。如果是端点则直接等于父亲数字的最优值加上它本身。- 求最后一行的最小值。
Solution
1 | public class Solution { |