Wednesday, September 24, 2014

Triangle

Problem

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

Idea

从下往上扫描, 递推公式: f(x,y) = min(f(x+1,y+1), f(x+1,y))+ triangle[x][y]
f(x,y)表示从底到点(x,y)的最短路长.

Solution


No comments:

Post a Comment