LeetCode·Daily Problem·931. Minimum Sum of Descent Path·Memorized Search

Author: Xiao Xun
Link: https://leetcode.cn/problems/minimum-falling-path-sum/solutions/2341965/ji-yi-hua-sou-suo-zhu-shi-chao-ji-xiang-3n58v/
Source: LeetCode
copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

 

example

 

train of thought

Title -> Given a two-dimensional array, return the path and minimum value from the first row to the last row

The simple and direct method is to find all the path sums, and then find the minimum value from all the path sums and return.

The above problem can be solved by using recursion, which is a depth-first search process. For each position, there are three next-step methods. Each time, the minimum value of the three methods is returned, and the minimum value is screened from bottom to top. The final return value must be min. Enumerate all starting positions and take the minimum value.

int dfs(int **matrix, int n, int m, int i, int j)
{
    if (j < 0 || j >= m) return INT_MAX;
    if (i == n-1) return  matrix[i][j];
    int left = INT_MAX, mid = INT_MAX, right = INT_MAX;
    left = dfs(matrix, n, m, i + 1, j - 1);
    mid = dfs(matrix, n, m, i + 1, j);
    right = dfs(matrix, n, m, i + 1, j + 1);
    return fmin(fmin(left, mid), right) + matrix[i][j];
}

For different starting positions, there are many same places in the middle distance, and the above method will still recursively enumerate the repeated places in the middle, and there are many repeated calculations. You can use an array to record the path traveled. When you arrive next time The value of the array record is returned directly at the current position, and the above method is a memory search.

Code comments are super detailed

the code


int dfs(int **matrix, int n, int m, int i, int j, int (*ans)[m])
{
    if (j < 0 || j >= m) return INT_MAX;//越界无效值
    if (ans[i][j] != INT_MAX) return ans[i][j];//重复路径
    if (i == n-1) return matrix[i][j];//底部返回
    int left = INT_MAX, mid = INT_MAX, right = INT_MAX;
    left = dfs(matrix, n, m, i + 1, j - 1, ans);//左边路径
    mid = dfs(matrix, n, m, i + 1, j,  ans);//中间路径
    right = dfs(matrix, n, m, i + 1, j + 1, ans);//右边路径
    //保存当前位置路径最小值,记忆化保存
    ans[i][j] = fmin(fmin(left, mid), right) + matrix[i][j];
    return  ans[i][j];
}
int minFallingPathSum(int** matrix, int matrixSize, int* matrixColSize){
    int min = INT_MAX;
    int n = matrixSize, m = matrixColSize[0];
    int ans[n][m];
    for (int i = 0; i < n; ++i) {//记忆化数组初始化
        for (int j = 0; j < m; ++j) {
            ans[i][j] = INT_MAX;
        }
    } 
    for (int i = 0; i < m; ++i) {//枚举每一个起点位置
        int sum = dfs(matrix, n, m, 0, i, ans);
        min = fmin(min, sum);//保存最小值路径
    }
    return min;
}

作者:小迅
链接:https://leetcode.cn/problems/minimum-falling-path-sum/solutions/2341965/ji-yi-hua-sou-suo-zhu-shi-chao-ji-xiang-3n58v/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/131696238