Leetcode:Minimum Path Sum 最小路径和

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.

此题解法同Triangle
数字三角形
。在此不再赘述。

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        size_t row = grid.size();
        if(row == 0)
            return 0;
        size_t col = grid[0].size();

        int **dp = new int *[row];
        for(size_t i = 0; i < row; i++)
        {
            dp[i] = new int[col];
        }
        dp[0][0] = grid[0][0];
        for(size_t i = 1; i < row; i++)
        {
            for(size_t j = 1; j < col; j++)
            {
                dp[i][j] = 0;
            }
        }

        for(size_t i = 1; i < col; i++)
            dp[0][i] = dp[0][i - 1] + grid[0][i];
        for(size_t i = 1; i < row; i++)
            dp[i][0] = dp[i - 1][0] + grid[i][0];

        for(size_t i = 1; i < row; i++)
            for(size_t j = 1; j < col; j++)
                dp[i][j] = min(dp[i][j - 1],dp[i - 1][j]) + grid[i][j];

        int result = dp[row - 1][col - 1];

        for(size_t i = 0; i < row; i++)
            delete []dp[i];
        delete []dp;

        return result;
    }
};
时间: 2024-07-29 04:47:03

Leetcode:Minimum Path Sum 最小路径和的相关文章

lintcode 容易题:Minimum Path Sum 最小路径和

题目: 最小路径和 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径. 样例 注意 你在同一时间只能向下或者向右移动一步 解题: 这个和求三角形的最小路径的差不多,这里是个矩阵,第一列和第一行要单独处理,每一点的值等于自身的值加上上一点的值,对于中间节点:grid[i][j] + = min( grid[i-1][j] , grid[i][j-1]) ,也就是说,i j 点的值只能是其左侧的点或者上侧的点加过来,这个时间复杂度比较高O(N2) Java程序: p

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: 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. 解题分析: 每次只能向下或者向

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: 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 r

LeetCode: Minimum Path Sum 解题报告

Minimum Path Sum 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. SOLUTION 1: 相当基础

LeetCode—Minimum Path Sum 二维数组最小路径,动态规划

感觉这是一系列的动态规划的算法,正好也将动态规划的算法进行一个总结: 算法一: 带权重的最小路径的问题 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. 首先每一个路径的上一个路径都是来自于其上方和左方 现将最上面的路径进行求和,最左边的路径进行求和

LeetCode 113. Path Sum II路径总和 II (C++)

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 Return: [ [5,4,11

LeetCode -- Minimum Path Sum

Question: 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. Analysis: 给出一个由非负数构成的 m

LeetCode (12) Path Sum (二叉树路径和判断)

题目描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, return true, as there exist a root-to-lea