In each cell a m × n board are placed a gift, each gift has a certain value (value greater than 0).
You can begin to get a gift from the upper left corner of the grid the board, and each time to the right or down until you reach the bottom right corner of a checkerboard grid.
Given a chessboard and above gift, calculate how much you can get the most valuable gift?
note:
m, n> 0
Sample:
Input:
[
[2,3,1],
[1,7,1],
[4, 6,]
]
Output: 19
Explanation: get the maximum value of the gift can be obtained along path 2 → 3 → 7 → 6 → 1.
DP does not explain naked
class Solution {
public:
int getMaxValue(vector<vector<int>>& grid) {
int n = 0,m = 0;n = grid.size();
if(n != 0) m = grid[0].size();
int dp[n + 1][m + 1];memset(dp,0,sizeof(dp));
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= m;j++) {
dp[i][j] = grid[i - 1][j - 1] + max(dp[i - 1][j],dp[i][j - 1]);
}
}
return dp[n][m];
}
};