博物馆大盗问题
tr = [None, {'w':2,"v":3}, {'w':3,"v":4}, {'w':4,"v":8}, {'w':5,"v":8}, {'w':9,"v":10}]
# 题目要求
max_w = 20
# 初始化表格
table=[]
for i in range(len(tr)):
table.append([0]*max_w)
for i in range(1, len(table)):
for w in range(max_w):
if tr[i]['w'] > w:
table[i][w]=table[i-1][w]
else:
table[i][w]=max(table[i-1][w],table[i-1][w-tr[i]['w']]+tr[i]['v'])
table
剑指 Offer 47. 礼物的最大价值
在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof
grid = [
[1,3,1],
[1,5,1],
[4,2,1]
]
m = len(grid)
n = len(grid[0])
for j in range(1, m):
grid[0][j] += grid[0][j-1]
for i in range(1, n):
grid[i][0] += grid[i-1][0]
for i in range(1, n):
for j in range(1, m):
grid[i][j] += max(grid[i][j-1], grid[i-1][j])
grid[-1][-1]