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.

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

Hide Tags

Array Dynamic Programming

Have you met this question in a real interview?

Yes

No

Discuss

这题是一个很简单的动态规划题

#include<iostream>
#include<vector>
using namespace std;
int minPathSum(vector<vector<int>>& grid) {
	int m=grid.size();
	int n=grid[0].size();
	int **ary=new int*[m];
	for(int i=0;i<m;i++)
		ary[i]=new int[n];

	ary[m-1][n-1]=grid[m-1][n-1];
	for(int i=n-2;i>=0;i--)
		ary[m-1][i]=grid[m-1][i]+ary[m-1][i+1];
	for(int i=m-2;i>=0;i--)
		ary[i][n-1]=grid[i][n-1]+ary[i+1][n-1];
	for(int i=m-2;i>=0;i--)
		for(int j=n-2;j>=0;j--)
		{
			ary[i][j+1]<ary[i+1][j]?ary[i][j]=ary[i][j+1]:ary[i][j]=ary[i+1][j];
			ary[i][j]=ary[i][j]+grid[i][j];
		}
	int a=ary[0][0];
	for(int i=0;i<m;i++)
		delete []ary[i];
	delete []ary;
	return a;
}
int main()
{

}

  

时间: 2024-10-06 04:31:54

leetcode_64题——Minimum Path Sum(动态规划)的相关文章

[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之“动态规划”: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 动态规划 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]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 --- 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 解题报告

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 &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