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
Have you met this question in a real interview?
Yes
No
这题是一个很简单的动态规划题
#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