The minimum path sum of triangles in LeetCode dynamic programming

Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

topic

Triangular minimum path sum

Given a triangle triangle, find the minimum top-down path sum.

Each step can only move to adjacent nodes in the next row. Adjacent nodes here refer to two nodes whose subscripts are the same as the subscripts of the nodes in the previous layer or equal to the subscripts of the nodes in the previous layer + 1. That is, if you are at index i of the current line, the next step can move to index i or i + 1 of the next line.

 

Example 1:

输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
输出:11
解释:如下面简图所示:
   2
  3 4
 6 5 7
4 1 8 3
自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
复制代码

Example 2:

输入:triangle = [[-10]]
输出:-10
复制代码

hint:

1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 10^4
复制代码

answer

problem-solving analysis

Problem solving ideas

  1. In this problem, the number of rows of a given triangle is n, and the i-th row (numbered from 00) contains the i+1number . If you align the left ends of each row, an isosceles right triangle is formed, like this:
[2] 
[3,4] 
[6,5,7] 
[4,1,8,3]
复制代码
  1. We use f[i][j] to denote the minimum sum of paths from the top of the triangle to position (i, j). The position (i, j) here refers to the position of row i and column j (both starting at 0) in the triangle. Since each line can only move to the [adjacent node] of the next line, if you want to go to position (i, j), the previous step can only be at position (i-1, j-1) or position (i - 1, j) . We choose the one with the smallest path among these two positions to transfer.

  2. State transition equation:

f[i][j] = min(f[i-1][j-1], f[i-1][j]) + c[u]
复制代码

Where c[i][j] represents the element value corresponding to the position (i, j).

Note: there are i + 1 elements on the i-th row, and they correspond to j in the range [0, i]. When j = 0 or j = i, some terms in the above state transition equation are meaningless. For example when j = 0, f[i-1][j-1] has no meaning, so the state transition equation is:

f[i][0] = f[i-1][0] + c[i][0]
复制代码
  1. That is, when we are at the far left of row i, we can only move from the far left of i-1row . When j=iis f[i-1]meaningless, so the state transition equation is:
f[i][i] = f[i-1][i-1] + c[i][i]
复制代码

That is, when we are on the far right of irow , we can only move from i-1the far right of row .

The final f[n-1][0]answer f[n-1][n-1]is the minimum of to , where n is the number of rows in the triangle.

  1. Note: What are the boundary conditions for the state transition equation? Since we have removed all "meaningless" states, the boundary conditions can be defined as:
f[0][0]=c[0][0]
复制代码

That is, at the top of the triangle, the minimum path sum is equal to the element value at the corresponding position. In this way, we can complete the calculation of all states by incrementally enumerating i starting from 1 and incrementally enumerating j in [0, i]the range of .

Complexity
Time complexity: O(N^2)
Space complexity: O(N^2)

problem solving code

The code for solving the problem is as follows (there are detailed comments in the code, please note that the C language is used to answer the question this time):

int minimumTotal(int** triangle, int triangleSize, int* triangleColSize){
    int f[2][triangleSize];
    memset(f, 0, sizeof(f));
    // 初始化值
    f[0][0] = triangle[0][0];

    for (int i=1; i < triangleSize; i++) {
        int curr = i %2;
        int prev = 1 - curr;
        f[curr][0] = f[prev][0] + triangle[i][0];
        for (int j = 1; j < i; j++) {
            f[curr][j] = fmin(f[prev][j-1], f[prev][j]) + triangle[i][j];
        }
        f[curr][i] = f[prev][i-1] +  triangle[i][i];
    }
    
    int ret = f[(triangleSize  -1) %2] [0];

    for (int i =1; i< triangleSize; i++) {
            ret = fmin(ret, f[(triangleSize - 1) %2][i]);
    }
    return ret;
}
复制代码

Feedback results after submission:

image.png

Reference Information

Guess you like

Origin juejin.im/post/7082758586987724807