...
/Greedy Knapsack Problem - Implementation
Greedy Knapsack Problem - Implementation
Learn how to implement the solution for the Greedy Knapsack Problem.
We'll cover the following...
We'll cover the following...
Implementation
We have already discussed the logic in Greedy Knapsack Problem lesson. Let’s directly jump to the code now.
Explanation:
- On line 4, we define a structure
Itemfor an item that stores weight and its corresponding value (profit). - On line 7, we create a constructor for our structure
Item, which will assign the profit and the weight of an Item. - In this code, we are going to use the built-in
sort()function to sort the items based on the