Daily Leetcode 120. Triangle
https://leetcode.com/problems/triangle/
问题描述:
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.
题目分析:
这一题乍一看,不注意约束条件,会觉得是一条easy难度的题目,就觉得是在等腰三角形中从上至下找一条最短和路径,然而题目中加上了约束条件:只能往下或者往右下移动。我们可以利用动态规划解决这种存在约束条件的路径求解问题。
从(i,j)
位置向下走得到的最短路径长度:
mp(i,j)=min{mp(i+1,j), mp(i+1,j+1)}+triangle(i,j)
我们可以从等腰三角形的底层向上一步步推,这样到最顶层就能够得到最短和路径,以原题所给示例为例子:
1 | mp(3,0) = 4 |
由上述推导,我们可以得出在这个例子中,最短和路径应该是2+3+5+1 = 11
代码:
1 | def minimumTotal(self, triangle): |