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.
For example, given the following triangle
1 | [ |
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.
解法
一道典型的动态规划题:
1 | dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]) + A[i][j] |
具体代码如下:
1 | class Solution { |
看到网上的大佬可以把二维降成一维的解题:
https://blog.csdn.net/fuxuemingzhu/article/details/82883187