Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e47975e

Browse files
committed
Create Pack-ZeroOnePack.py
1 parent fdec903 commit e47975e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def zeroOnePack(self, weight: [int], value: [int], W: int):
3+
size = len(weight)
4+
dp = [[0 for _ in range(W + 1)] for _ in range(size + 1)]
5+
6+
for i in range(1, size + 1):
7+
for w in range(weight[i], W + 1):
8+
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weight[i]] + value)
9+
10+
return dp[size]
11+
12+
def zeroOnePackOptimization(self, weight: [int], value: [int], W: int):
13+
size = len(weight)
14+
dp = [0 for _ in range(W + 1)]
15+
16+
for i in range(1, size + 1):
17+
for w in range(W, weight[i] - 1, -1):
18+
dp[w] = max(dp[w], dp[w - weight[i]] + value[i])
19+
20+
return dp[size]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /