Chinese Description:
Given a number triangle, find the smallest path from the top to the bottom and the. Each step can be moved to an adjacent number on the following line.
Examples
For example, give the following number triangles:
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
Minimum path from top to bottom and 11 (2 + 3 + 5 + 1 = 11).
Attention
If you do this with only the extra space complexity O (n), you can get a bonus, where n is the total number of rows in the number triangle.
中文版 Version:
Given a triangle, find the minimum path sum from top to bottom. Each step you'll move to adjacent numbers on the row below.
Example
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum is one bottom (i.e., 2 + 3 + 5 + 1 = 11).
Note
Bonus Point if your are able to does this using only O (n) extra spaces, where n is the total number of rows in the triangle.
public class Solution {/** * @param triangle:a List of lists of integers.
* @return: An integer, Minimum path sum. ///////////from top to bottom minimum path and equal to minimum path and////To start from penultimate layer, the minimum path length of each number from the bottom to each layer equals the smaller value in the minimum path length of the lower adjacent digits from the bottom to the layer, plus the value of the number in that layer public I NT Minimumtotal (arraylist<arraylist<integer>> triangle) {list<integer> List = new ArrayList<
Integer> ();
for (int i = Triangle.size ()-2; I >= 0; i--) {for (int j = 0; J < Triangle.get (i). Size (); j + +) {
int value = Math.min (Triangle.get (i+1). Get (j), Triangle.get (i+1). Get (j+1)) + triangle.get (i). get (j);
Triangle.get (i). Set (J,value);
} return Triangle.get (0). Get (0); }
}