comments | difficulty | edit_url |
---|---|---|
true |
中等 |
在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?
示例 1:
输入:[ [1,3,1], [1,5,1], [4,2,1] ]
输出:12
解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物
提示:
0 < grid.length <= 200
0 < grid[0].length <= 200
我们定义
答案为
时间复杂度
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
f = [[0] * (n + 1) for _ in range(m + 1)]
for i, row in enumerate(grid, 1):
for j, v in enumerate(row, 1):
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + v
return f[m][n]
class Solution {
public int maxValue(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m][n];
}
}
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> f(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m][n];
}
};
func maxValue(grid [][]int) int {
m, n := len(grid), len(grid[0])
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
f[i][j] = max(f[i-1][j], f[i][j-1]) + grid[i-1][j-1]
}
}
return f[m][n]
}
function maxValue(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const f = Array.from({ length: m + 1 }, _ => new Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m][n];
}
impl Solution {
pub fn max_value(mut grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let m = grid[0].len();
for i in 1..n {
grid[i][0] += grid[i - 1][0];
}
for i in 1..m {
grid[0][i] += grid[0][i - 1];
}
for i in 1..n {
for j in 1..m {
grid[i][j] += grid[i][j - 1].max(grid[i - 1][j]);
}
}
grid[n - 1][m - 1]
}
}
/**
* @param {number[][]} grid
* @return {number}
*/
var maxValue = function (grid) {
const m = grid.length;
const n = grid[0].length;
const f = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m][n];
};
public class Solution {
public int MaxValue(int[][] grid) {
int m = grid.Length, n = grid[0].Length;
int[, ] f = new int[m + 1, n + 1];
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
f[i, j] = Math.Max(f[i - 1, j], f[i, j - 1]) + grid[i - 1][j - 1];
}
}
return f[m, n];
}
}
class Solution {
func maxValue(_ grid: [[Int]]) -> Int {
let m = grid.count
let n = grid[0].count
var f = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
for i in 1...m {
for j in 1...n {
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1]
}
}
return f[m][n]
}
}
我们注意到
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
f = [[0] * (n + 1) for _ in range(2)]
for i, row in enumerate(grid, 1):
for j, v in enumerate(row, 1):
f[i & 1][j] = max(f[i & 1 ^ 1][j], f[i & 1][j - 1]) + v
return f[m & 1][n]
class Solution {
public int maxValue(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] f = new int[2][n + 1];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
f[i & 1][j] = Math.max(f[i & 1 ^ 1][j], f[i & 1][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m & 1][n];
}
}
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> f(2, vector<int>(n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
f[i & 1][j] = max(f[i & 1 ^ 1][j], f[i & 1][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m & 1][n];
}
};
func maxValue(grid [][]int) int {
m, n := len(grid), len(grid[0])
f := make([][]int, 2)
for i := range f {
f[i] = make([]int, n+1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
f[i&1][j] = max(f[i&1^1][j], f[i&1][j-1]) + grid[i-1][j-1]
}
}
return f[m&1][n]
}
function maxValue(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const f = Array.from({ length: 2 }, _ => new Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i & 1][j] = Math.max(f[(i & 1) ^ 1][j], f[i & 1][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m & 1][n];
}
/**
* @param {number[][]} grid
* @return {number}
*/
var maxValue = function (grid) {
const m = grid.length;
const n = grid[0].length;
const f = new Array(2).fill(0).map(() => new Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i & 1][j] = Math.max(f[(i & 1) ^ 1][j], f[i & 1][j - 1]) + grid[i - 1][j - 1];
}
}
return f[m & 1][n];
};