class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
for(int i = 1;i < grid.size();i++){
grid[i][0] += grid[i-1][0];
}
for(int j = 1;j<grid[0].size();j++){
grid[0][j] += grid[0][j-1];
}
for(int i = 1;i < grid.size();i++){
for(int j = 1;j<grid[0].size();j++){
grid[i][j] += max(grid[i][j-1],grid[i-1][j]);
}
}
return grid[grid.size()-1][grid[0].size()-1];
}
};
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
for i in range(1,len(grid)):
grid[i][0] += grid[i-1][0]
for j in range(1,len(grid[0])):
grid[0][j] += grid[0][j-1]
for i in range(1,len(grid)):
for j in range(1,len(grid[0])):
grid[i][j] += max(grid[i][j-1],grid[i-1][j])
return grid[-1][-1]
|