- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find maximum amount we can get by taking different items within the capacity in Python
Suppose we have two lists called weights and values which are of same length and another number called capacity k. Here weights[i] and values[i] shows the weight and value of the ith item. Now, we can take at most k capacity weights, and that we can only take at most one copy of each item, we have to find the maximum amount of value we can get.
So, if the input is like weights = [2, 3, 4], values = [2, 6, 4], capacity = 6, then the output will be 8
To solve this, we will follow these steps −
- n:= size of weights
- dp:= a matrix of size capacity x n, and fill with 0
- for i in range 0 to n, do
- for j in range 0 to capacity, do
- if i is same as 0 or j is same as 0, then
- dp[i, j]:= 0
- otherwise when weights[i-1] <= j, then
- dp[i,j] = maximum of (dp[i-1, j-weights[i - 1]] + values[i-1]) and (dp[i-1, j])
- otherwise,
- dp[i, j]:= dp[i-1, j]
- if i is same as 0 or j is same as 0, then
- for j in range 0 to capacity, do
- return dp[n, capacity]
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, weights, values, capacity): n=len(weights) dp=[[0 for i in range(capacity+1)] for _ in range(n+1)] for i in range(n+1): for j in range(capacity+1): if i==0 or j==0: dp[i][j]=0 elif weights[i-1]<=j: dp[i][j]=max(dp[i-1][j-weights[i-1]]+values[i-1],dp[i-1][j]) else: dp[i][j]=dp[i-1][j] return dp[n][capacity] ob = Solution() weights = [2, 3, 4] values = [2, 6, 4] capacity = 6 print(ob.solve(weights, values, capacity))
Input
[2, 3, 4], [2, 6, 4], 6
Output
8
- Related Articles
- Program to find maximum value we can get in knapsack problem by taking multiple copies in Python
- Program to find maximum price we can get by holding items into a bag in Python
- Program to find maximum credit we can get by finishing some assignments in python
- Program to find the maximum profit we can get by buying on stock market once in Python
- Program to find maximum score we can get in jump game in Python
- Program to find the maximum profit we can get by buying on stock market multiple times in Python
- Program to find maximum amount of coin we can collect from a given matrix in Python
- Program to find maximum number of coins we can get using Python
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Program to find how many total amount of rain we can catch in Python
- Program to find maximum number of coins we can collect in Python

Advertisements