Given a triangle, find the minimum path sum from top to bottom. Each step of the 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 11 is (i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus Point If-able to does this using only O(n) extra space, where n is the total number of rows in the triangle.
The value of the next layer
MINPATH[I][J] + = min (minpath[i-1][j-1], minpath[i-1][j]);
1 intMinimumtotal (vector<vector<int> > &triangle)2 {3vector<vector<int> >Minpath (triangle);4 intMin_path =Int_max, I, J;5 6 for(i =1; I < triangle.size (); i++)7 {8 for(j =0; J <= I; J + +)9 {Ten if(J = =0) OneMINPATH[I][J] + = minpath[i-1][j]; A Else if(J = =i) -MINPATH[I][J] + = minpath[i-1][j-1]; - Else theMINPATH[I][J] + = min (Minpath[i-1][j-1], Minpath[i-1][j]); - } - } - + for(j =0; J < I; J + +) -Min_path = min (Min_path, Minpath[i-1][j]); + A returnMin_path; at}
Leetcode. Triangle