题解 | #礼物的最大价值#
礼物的最大价值
https://www.nowcoder.com/practice/2237b401eb9347d282310fc1c3adb134
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param grid int整型二维数组
* @return int整型
*/
export function maxValue(grid: number[][]): number {
// write code here
//初始化
let row = grid.length
let colume = grid[0].length
//第一行,只能来自左边,所以他们的值为
for(let i = 1;i < colume;i++){
//进行初始化
grid[0][i] += grid[0][i-1]
}
for(let j = 1;j < row;j++){
grid[j][0] += grid[j-1][0]
}
//进行遍历
for(let i = 1;i < row;i++){
for(let j = 1;j < colume;j++){
grid[i][j] += Math.max(grid[i-1][j] , grid[i][j-1])
}
}
return grid[row-1][colume-1]
}
