This question is given a triangular array, find the top-down one path, such that the elements of the path and the minimum, we first look at the title of this question:
First of all we get this problem, we will think of the recursive method, because we can only choose every time down the adjacent elements, then we recursive equation can be written as:
triangle(i,j){
triangle(i, j + 1);
triangle(i + 1, j + 1)
}
We can use this method recursively through all the paths, we can the elements of each path recorded and then select the smallest one, that is the purpose of this question solution, time complexity of this solution was
. This is clearly not the best solution, then we can consider thegreedy algorithm, each select their own and adjacent to the smallest element, think carefully, this algorithm is clearly global optimal solution can not be met, the most easy to fall into local optimal solution, then the greedy algorithm is not desirable. Finally, we can expect the use ofdynamic programmingalgorithm to analyze the question head: First, we consider the bottom-up, we will first
optimum solution in mind for the elements
, then
is equivalent to
neighboring locations
function plus
element itself on the place, so the whole dynamic programming recurrence equation can be written as:
Once you have this dynamic programming equation, we go from the bottom up recursive final topmost
is the result of our requirements. Let's look at a specific example, the following is an example of a triangle to:
We DP by the function value of dynamic programming is started bottom layer, the bottom layer can be determined location:
can be obtained, the bottom layer
value is the element itself, then we can continue upwards
:
see penultimate layer, each layer
function value equal to the value of the minimum elements with their own adjacent plus the value of their own, the same way we can continue up
:
until the second layer, eventually we
to
The position we can find a solution:
in the end we can find the answer to this topic
, the flow of the algorithm should be said to be very clear, the complexity of the algorithm is
, the following look at the code:
java:
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle.size() == 0 || triangle.get(0).size() == 0)
return 0;
for(int i = triangle.size() - 2; i >= 0; --i){
for(int j = triangle.get(i).size() - 1; j >= 0; --j){
int min = Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1));
min += triangle.get(i).get(j);
triangle.get(i).set(j, min);
}
}
return triangle.get(0).get(0);
}
}
python:
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
if not triangle:
return 0
res = triangle[-1]
for i in range(len(triangle) - 2, -1, -1):
for j in range(len(triangle[i])):
res[j] = min(res[j], res[j+1])+ triangle[i][j]
return res[0]
This question is a very classic subject of dynamic programming, and I hope we can be very thorough understanding of the idea of dynamic programming through this blog, thank you.