leetcode No64. 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.

Algorithm:

比较从左边来的路径和和从上面来的路径谁小,再加上当前元素,还是用动态规划

Accepted Code:

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int M=grid.size();
        int N=grid[0].size();
        if(M==0) return 0;
        vector<vector<int>> res(grid);
        for(int j=1;j<N;j++)    //先要把边界的算出来
            res[0][j]+=res[0][j-1];
        for(int i=1;i<M;i++)
            res[i][0]+=res[i-1][0];
        for(int i=1;i<M;i++)
            for(int j=1;j<N;j++)
            {
                res[i][j]=min(res[i-1][j],res[i][j-1])+res[i][j];
            }
        return res[M-1][N-1];
    }
};
时间: 2024-10-12 09:05:29

leetcode No64. Minimum Path Sum的相关文章

LeetCode --- 64. 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. 这道题的要求是在m*n

【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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

LeetCode 64. 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. 题目标签:Array 这道题目和前面两题差不多,基本思想都是一样的

[LeetCode][Java] 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. 题意: 给定一个m x n 大小的网格结构,网格中包含非负

LeetCode 64. Minimum Path Sum 20170515

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. 题目大意:给定一个一个m*n格的方针,每个方格都放一个非负数,找到

LeetCode 63 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. 思路 :求grid[0][0]到grid[i][j]的最小和,就必

LeetCode 64. Minimum Path Sum Java

题目: 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. 题意及分析:给出一个m*n大的网格,每个网格中的数字都为非

【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. [思路] 求从左上角到右下角的最小路径值,典型的动态规划