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.
Ideas:
Can only move down and to the right, it is very simple. Dynamic planning, current min and = current value + current point above or left smallest and, note the boundary condition.
Exercises
classSolution { Public: intMinpathsum (vector<vector<int> > &grid) { introw =grid.size (); intCol = grid[0].size (); if(row==0|| col==0) return 0; int**DP =New int*[row]; for(intI=0; i<row;i++) Dp[i]=New int[col]; for(intI=0; i<row;i++) for(intj=0; j<col;j++) { if(i==0&& j==0) dp[0][0] = grid[0][0]; Else if(i==0) dp[0][J] = grid[0][j]+dp[0][j-1]; Else if(j==0) dp[i][0] = grid[i][0]+dp[i-1][0]; ElseDp[i][j]= Grid[i][j]+min (dp[i-1][J], dp[i][j-1]); } returndp[row-1][col-1]; }};
[Leetcode] Minimum Path Sum