![]() |
VOOZH | about |
Given two arrays weight[] and profit[] the weights and profit of N items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
Note: Unlike 0/1 knapsack, you are allowed to break the item.
Examples:
Input: weight[] = {10, 20, 30}, profit[] = {60, 100, 120}, N= 50 Output: Maximum profit earned = 240 Explanation:
Decreasing p/w ratio[] = {6, 5, 4} Taking up the weight values 10, 20, (2 / 3) * 30
Profit = 60 + 100 + 120 * (2 / 3) = 240Input: weight[] = {10, 40, 20, 24}, profit[] = {100, 280, 120, 120}, N = 60 Output: Maximum profit earned = 440 Explanation:
Decreasing p/w ratio[] = {10, 7, 6, 5} Taking up the weight values 10, 40, (1 / 2) * 120
Profit = 100 + 280 + (1 / 2) * 120 = 440
Method 1 - without using STL: The idea is to use Greedy Approach. Below are the steps:
Below is the implementation of the above approach:
Maximum profit earned = 440
Time Complexity: O(N*log2N)
Auxiliary Space: O(1)
Method 2 - using STL:
Below is the implementation of the above approach:
Maximum profit earned is:440
Time Complexity: O(N)
Auxiliary Space: O(N)