码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-08 11:07:16      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

Minimum Path Sum

 Total Accepted: 38669 Total Submissions: 120082My 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 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()
{

}

  

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

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4560145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!