• [Leetcode Week9]Minimum Path Sum


    Minimum Path Sum 题解

    原创文章,拒绝转载

    题目来源:https://leetcode.com/problems/minimum-path-sum/description/


    Description

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

    Note: You can only move either down or right at any point in time.

    Example 1:

    [[1,3,1],
     [1,5,1],
     [4,2,1]]
    

    Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

    Solution

    class Solution {
    public:
        int minPathSum(vector<vector<int> >& grid) {
            if (grid.empty())
                return 0;
            int i, j;
            int rowCount = grid.size();
            int colCount = grid[0].size();
            int **stepCount;
            stepCount = new int*[rowCount];
    
            stepCount[0] = new int[colCount];
            stepCount[0][0] = grid[0][0];
            for (i = 1; i < rowCount; i++) {
                stepCount[i] = new int[colCount];
                stepCount[i][0] = grid[i][0] + stepCount[i - 1][0];
            }
            for (j = 1; j < colCount; j++) {
                stepCount[0][j] = grid[0][j] + stepCount[0][j - 1];
            }
    
            for (i = 1; i < rowCount; i++) {
                for (j = 1; j < colCount; j++) {
                    stepCount[i][j] = min(stepCount[i - 1][j], stepCount[i][j - 1]) + grid[i][j];
                }
            }
            j = stepCount[rowCount - 1][colCount - 1];
            for (i = 0; i < rowCount; i++)
                delete [] stepCount[i];
            delete [] stepCount;
            return j;
        }
    };
    

    解题描述

    这道题其实本质上类似于经典的动态规划问题中的最小编辑距离问题,大概的方法还是差不多的,通过一个矩阵去计算所有的状态下的步数,最后返回右下角的点的步数即为最小的步数之和。

  • 相关阅读:
    Brackets_区间DP
    石子合并_区间Dp
    You Are the One_区间DP
    Palindrome subsequence_区间DP
    Infix to postfix 用stack模板,表达式没有括号
    Java 4
    Java 3
    规范化的递归转换成非递归
    recursion 递归以及递归的缺点
    Infix to postfix conversion 中缀表达式转换为后缀表达式
  • 原文地址:https://www.cnblogs.com/yanhewu/p/7768414.html
Copyright © 2020-2023  润新知