LeetCode 120. triangular minimum path and

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Oscar6280868/article/details/89563570

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:
triangle
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 O ( 2 n ) O (2 ^ n) . 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 i , j i,j optimum solution in mind for the elements D P ( i , j ) DP(i,j) , then D P ( i , j ) DP(i,j) is equivalent to i , j i,j neighboring locations D P DP function plus i , j i, j element itself on the place, so the whole dynamic programming recurrence equation can be written as: D P ( i , j ) = m i n ( D P ( i , j + 1 ) , D P ( i + 1 , j + 1 ) ) + t r i a n g l e ( i , j ) DP(i, j)=min(DP(i,j+1),DP(i+1,j+1))+triangle(i,j)
Once you have this dynamic programming equation, we go from the bottom up recursive final topmost D P ( 0 , 0 ) DP(0,0) is the result of our requirements. Let's look at a specific example, the following is an example of a triangle to:
triangle
We DP by the function value of dynamic programming is started bottom layer, the bottom layer can be determined location:
triangle2
can be obtained, the bottom layer D P DP value is the element itself, then we can continue upwards D P DP :
triangle
see penultimate layer, each layer D P DP 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 D P DP :
triangle
until the second layer, eventually we D P DP to ( 0 , 0 ) (0,0) The position we can find a solution:
triangle
in the end we can find the answer to this topic D P ( 0 , 0 ) = 11 DP(0,0)=11 , the flow of the algorithm should be said to be very clear, the complexity of the algorithm is O ( i j ) It (the government) , 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 D P DP 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.

Guess you like

Origin blog.csdn.net/Oscar6280868/article/details/89563570