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 you be able to does this using only O(n) extra space, where n is the total Number of rows in the triangle.
"Problem Analysis"
Given a triangle, the structure of the triangle and the example given in the topic are the same, find a path from the top to the bottom of the triangle, and make the path pass through the smallest element. Each step can only be moved to the next row and adjacent to the current element.
Ideas
1. Ways to change triangle elements
Since it is moving from top to bottom, we can add the value of the elements on the previous layer to the next layer of adjacent elements, if the next layer of an element in the previous layer has two adjacent elements, that the two elements of the smaller one added to the next layer of elements. The process is as follows:
---> or
We can pick the smallest value from the last line, and of course it can be reversed, from the last line up, and the upper element from the two lower-level elements adjacent to it.
2. How the triangle element does not change
The idea of this method is the same as the previous method, except that the method does not change the elements in the triangle, but rather maintains an array to store the change. For example, from the bottom up of the traversal process, the last line is assigned to the array, from the adjacent elements of the array to select the smaller one plus the two elements in the previous layer of the common adjacent elements, the new value is still stored in the array. At this point the space complexity is O (n).
"Java code 1" traverses from top to bottom, triangle element changes
1 Public classSolution {2 Public intMinimumtotal (list<list<integer>>triangle) {3 if(Triangle = =NULL|| Triangle.size () = = 0)return0;4 intTriszie =triangle.size ();5 6List<integer> clist = triangle.get (0);7 for(inti = 0; i < triszie-1; i++){8list<integer> nlist = Triangle.get (i+1);9Nlist.set (0, clist.get (0) + nlist.get (0));Ten for(intj = 1; J <= I; J + +){ One intValue = Math.min (Clist.get (j), Clist.get (j-1)) +Nlist.get (j); A Nlist.set (J, value); - } -Nlist.set (i+1, Clist.get (i) + nlist.get (i+1)); theCList =nlist; - } - - intmin = clist.get (0); + for(inti = 1; I < clist.size (); i++){ -Min =math.min (min, Clist.get (i)); + } A returnmin; at } -}
"Java code 2" from the bottom up, the triangle element does not change
1 Public classSolution {2 Public intMinimumtotal (list<list<integer>>triangle) {3 if(Triangle = =NULL|| Triangle.size () = = 0)return0;4 intTriszie =triangle.size ();5 int[] DP =New int[Triszie];6 7 for(inti = triszie-1; I >= 0; i--){8 for(intj = 0; J <= I; J + +){9 if(i = = triszie-1) Dp[j] =Triangle.get (i). get (j);Ten ElseDP[J] = Math.min (dp[j],dp[j+1]) +Triangle.get (i). get (j); One } A } - returnDp[0]; - } the}
Leetcode OJ 120. Triangle