Leetcode 120. 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

[
     [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.

分析

找出一个三角形从顶到底最短路径。很简单的动态规划问题,从上到下,依次计算到当前行左边路线和右边路线哪个是最短距离。不过需要两端的点只有一条路,需要另处理。
当然也可以从下到上,更方面了。

int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes) {
    int ans=0;
    for(int i=1;i

你可能感兴趣的:(Leetcode 120. Triangle)