狀態轉移方程dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]),i代表第幾個物品,j代表揹包容量,w[i - 1],v[i - 1]代表第i個物品的重量和價值。
#揹包問題i
class
solution
:"""
@param m: an integer m denotes the size of a backpack
@param a: given n items with size a[i]
@return: the maximum size
"""defbackpack
(self, m, a)
:# write your code here
dp =[0
for _ in
range
(m +1)
]for i in
range
(len
(a))
:for j in
range
(m, a[i]-1
,-1)
: dp[j]
=max
(dp[j]
, dp[j - a[i]
]+ a[i]
)return dp[m]
#n = len(a)
#dp = [[0] * (m + 1) for _ in range(n + 1)]
#for i in range(1, n + 1):
# for j in range(1, m + 1):
# dp[i][j] = dp[i - 1][j]
# if j >= a[i - 1]:
# dp[i][j] = max(dp[i-1][j], dp[i-1][j - a[i - 1]] + a[i - 1])
#return dp[n][m]
#揹包問題ii
class
solution
:"""
@param m: an integer m denotes the size of a backpack
@param a: given n items with size a[i]
@param v: given n items with value v[i]
@return: the maximum value
"""defbackpackii
(self, m, a, v)
:# write your code here
#滾動陣列:時間o(am)空間o(m)
#dp = [0 for _ in range(m + 1)]
#n = len(a)
#for i in range(n):
# for j in range(m, a[i]-1, -1):
# dp[j] = max(dp[j], dp[j - a[i]] + v[i])
#return dp[m]
#時間複雜度o(am)空間o(am)
dp =[[
0]*(m +1)
for _ in
range
(len
(a)+1)
]for i in
range(1
,len
(a)+1)
:for j in
range(1
, m +1)
:if a[i -1]
> j:
dp[i]
[j]= dp[i -1]
[j]else
: dp[i]
[j]=
max(dp[i -1]
[j], dp[i-1]
[j - a[i-1]
]+ v[i -1]
)return dp[
len(a)
][m]
# 揹包問題v
class
solution
:"""
@param nums: an integer array and all positive numbers
@param target: an integer
@return: an integer
"""defbackpackv
(self, nums, target)
:# write your code here
#n = len(nums)
#dp = [[0] * (target + 1) for _ in range(n + 1)]
#dp[0][0] = 1
#for i in range(1, n + 1):
# for j in range(target + 1):
# dp[i][j] = dp[i - 1][j]
# if j >= nums[i - 1]:
# dp[i][j] += dp[i - 1][j - nums[i - 1]]
#return dp[n][target]
#滾動陣列
n =len(nums)
dp =[0
for _ in
range
(target +1)
] dp[0]
=1for i in
range
(n):
for j in
range
(target,-1
,-1)
:if j >= nums[i]
: dp[j]
+= dp[j - nums[i]
]return dp[target]
#揹包問題 ix
class
solution
:"""
@param n: your money
@param probability: probability of getting the university's offer
@return: the highest probability
"""defbackpackix
(self, n, prices, probability)
:# write your code here
p =len(prices)
dp =[0
for _ in
range
(n +1)
]for i in
range
(p):
for j in
range
(n, prices[i]-1
,-1)
: dp[j]
=max
(dp[j],1
-(1- dp[j - prices[i]])
*(1- probability[i]))
return
float
(dp[n]
)
#卡牌遊戲ii
class
solution
:"""
@param cost: costs of all cards
@param damage: damage of all cards
@param totalmoney: total of money
@param totaldamage: the damage you need to inflict
@return: determine if you can win the game
"""defcardgame
(self, cost, damage, totalmoney, totaldamage)
:# write your code here
n =len(cost)
dp =[0
for _ in
range
(totalmoney +1)
]for i in
range
(n):
for j in
range
(totalmoney, cost[i]-1
,-1)
: dp[j]
=max
(dp[j]
, dp[j - cost[i]
]+ damage[i]
)return dp[totalmoney]
>= totaldamage
LintCode 揹包問題
揹包問題 在n個物品中挑選若干物品裝入揹包,最多能裝多滿?假設揹包的大小為m,每個物品的大小為a i 您在真實的面試中是否遇到過這個題?yes 樣例如果有4個物品 2,3,5,7 如果揹包的大小為11,可以選擇 2,3,5 裝入揹包,最多可以裝滿10的空間。如果揹包的大小為12,可以選擇 2,3,7...
揹包問題 LintCode
在n個物品中挑選若干物品裝入揹包,最多能裝多滿?假設揹包的大小為m,每個物品的大小為a i 樣例 如果有4個物品 2,3,5,7 如果揹包的大小為11,可以選擇 2,3,5 裝入揹包,最多可以裝滿10的空間。如果揹包的大小為12,可以選擇 2,3,7 裝入揹包,最多可以裝滿12的空間。函式需要返回最...
揹包問題 LintCode
首次在lintcode上做了乙個打敗了100 提交的答案,一定要紀念一下 下面進入正題 揹包問題 lintcode 問題描述 在n個物品中挑選若干物品裝入揹包,最多能裝多滿?假設揹包的大小為m,每個物品的大小為a i 注意事項 不可以將物品進行切割。樣例說明 如果有4個物品 2,3,5,7 如果揹包...