动态规划之 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.

假定gird用于保存m*n数字,

dp[i][j]表示到第i行第j列的最小路径和,则每次往右或者往下走,则得到递推公式

dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j]

初始值:

i=0,j=0  dp[i][j]=grid[i][j];

i=0,j>0  此时只能从左往右走,则dp[i][j]=dp[i][j-1]+grid[i][j];

i>0,j=0  此时只能从上往下走,则dp[i][j]=dp[i-1][j]+grid[i][j];

int minPathSum(vector<vector<int>>& grid) {

int m=grid.size();

if(m==0)

return -1;

int n=grid[0].size();

vector<vector<int> > dp(m,vector<int>(n));

for(int i=0;i<m;i++){

for(int j=0;j<n;j++){

if(i==0){

if(j==0){

dp[i][j]=grid[i][j];

}

else{

dp[i][j]=dp[i][j-1]+grid[i][j];

}

}

else{

if(j==0){

dp[i][j]=dp[i-1][j]+grid[i][j];

}

else{

dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];

}

}

}

}

return dp[m-1][n-1];

}

优化空间

用一维数组代替二维数组,

由dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j] 知

dp[i][j]和dp[i-1][j],dp[i][j-1],grid[i][j]有关,这时如果计算顺序是正向,下标从小开始,

则在第i次循环,计算dp[j] 时,dp[i-1][j]可以看作是dp[j]的旧值,即第i-1次循环时的值,此时已经计算完成,dp[i][j-1]可以看作dp[j-1]

则可以更新为

dp[j]=min(dp[j-1],dp[j])+grid[i][j]

在代码中直接把dp[i][j] ==>dp[j]  dp[i-1][j]==>dp[j]

vector<int> dp(n);

for(int i=0;i<m;i++){

for(int j=0;j<n;j++){

if(i==0){

if(j==0){

dp[j]=grid[i][j];

}

else{

dp[j]=dp[j-1]+grid[i][j];

}

}

else{

if(j==0){

dp[j]=dp[j]+grid[i][j];

}

else{

dp[j]=min(dp[j],dp[j-1])+grid[i][j];

}

}

}

}

return dp[n-1];

时间: 2024-11-10 03:23:27

动态规划之 minimum path sum的相关文章

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. 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 righ

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to rea

leetcode_64题——Minimum Path Sum(动态规划)

Minimum Path Sum Total Accepted: 38669 Total Submissions: 120082My Submissions Question Solution 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. Not

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

题目链接: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 矩形网格最小路径和

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]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: 相当基础