标签:style blog color io strong for div sp 代码
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.
思路:看到这道题想到两种方法,第一种是分支限界,不过时间复杂度比较高,估计会超时,另一种方法是动态规划,时间复杂度为O(m*n,代码如下:
1 class Solution { 2 public: 3 vector<vector<int>> mi; 4 int minPathSum(vector<vector<int> > &grid) { 5 mi=grid; 6 for(int i=1;i<grid[0].size();i++) 7 { 8 mi[0][i]=mi[0][i-1]+mi[0][i]; 9 } 10 for(int i=1;i<grid.size();i++) 11 { 12 mi[i][0]=mi[i-1][0]+mi[i][0]; 13 for(int j=1;j<grid[0].size();j++) 14 { 15 mi[i][j]=min(mi[i-1][j],mi[i][j-1])+mi[i][j]; 16 } 17 } 18 return mi[grid.size()-1][grid[0].size()-1]; 19 } 20 };
标签:style blog color io strong for div sp 代码
原文地址:http://www.cnblogs.com/sqxw/p/3960163.html