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.
Test instructions: Ask for the smallest cost from the upper left to the lower right corner.
Idea: a simple DP.
Class Solution {public: int f[1000][1000]; int Minpathsum (vector<vector<int> > &grid) { if (grid.size () = = 0 | | grid[0].size () = = 0) return 0 ; F[0][0] = grid[0][0]; for (int i = 1; i < grid[0].size (); i++) f[0][i] = f[0][i-1] + grid[0][i]; for (int i = 1; i < grid.size (); i++) f[i][0] = f[i-1][0] + grid[i][0]; for (int i = 1; i < grid.size (); i++) for (int j = 1; J < grid[0].size (); + j) F[i][j] = min (f[i-1][j], f[ I][J-1]) + grid[i][j]; Return F[grid.size () -1][grid[0].size ()-1]; };
Leetcdoe Minimum Path Sum