FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python/dynamic_programming/knapsack.py at master · vivi-coding/Python · GitHub
vivi-coding
/
Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Python
/
dynamic_programming
/
knapsack.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
14 lines (12 loc) · 455 Bytes
Breadcrumbs
Python
/
dynamic_programming
/
knapsack.py
Copy path
File metadata and controls
14 lines (12 loc) · 455 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
"""
def
knapsack
(
W
,
wt
,
val
,
n
):
dp
=
[[
0
for
i
in
range
(
W
+
1
)]
for
j
in
range
(
n
+
1
)]
for
i
in
range
(
1
,
n
+
1
):
for
w
in
range
(
1
,
W
+
1
):
if
(
wt
[
i
-
1
]
<=
w
):
dp
[
i
][
w
]
=
max
(
val
[
i
-
1
]
+
dp
[
i
-
1
][
w
-
wt
[
i
-
1
]],
dp
[
i
-
1
][
w
])
else
:
dp
[
i
][
w
]
=
dp
[
i
-
1
][
w
]
return
dp
[
n
][
w
]
Back
|
FazBrowse Home
|
New Git URL