题解 | #礼物的最大价值#
礼物的最大价值
http://www.nowcoder.com/practice/2237b401eb9347d282310fc1c3adb134
#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param grid int整型二维数组
# @return int整型
#
class Solution:
def maxValue(self , grid ):
# write code here
m = len(grid)
n = len(grid[0])
gArr =[[0 for item in range(n) ] for item in range(m)]
gArr[0][0]= grid[0][0]
for i in range(m):
for j in range(n):
up = 0
left = 0
if i > 0:
up = gArr[i-1][j]
if j > 0:
left = gArr[i][j-1]
gArr[i][j] = max(up, left) + grid[i][j]
ret = gArr[m-1][n-1]
return ret
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param grid int整型二维数组
# @return int整型
#
class Solution:
def maxValue(self , grid ):
# write code here
m = len(grid)
n = len(grid[0])
gArr =[[0 for item in range(n) ] for item in range(m)]
gArr[0][0]= grid[0][0]
for i in range(m):
for j in range(n):
up = 0
left = 0
if i > 0:
up = gArr[i-1][j]
if j > 0:
left = gArr[i][j-1]
gArr[i][j] = max(up, left) + grid[i][j]
ret = gArr[m-1][n-1]
return ret