forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFractional Knapsack.cpp
79 lines (68 loc) · 1.66 KB
/
Fractional Knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Fractional Knapsack
===================
Given weights and values 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.
Example 1:
Input:
N = 3, W = 50
values[] = {60,100,120}
weight[] = {10,20,30}
Output:
240.00
Explanation:Total maximum value of item
we can have is 240.00 from the given
capacity of sack.
Example 2:
Input:
N = 2, W = 50
values[] = {60,100}
weight[] = {10,20}
Output:
160.00
Explanation:
Total maximum value of item
we can have is 160.00 from the given
capacity of sack.
Your Task :
Complete the function fractionalKnapsack() that receives maximum capacity , array of structure/class and size n and returns a double value representing the maximum value in knapsack.
Note: The details of structure/class is defined in the comments above the given function.
Expected Time Complexity : O(NlogN)
Expected Auxilliary Space: O(1)
Constraints:
1 <= N <= 105
1 <= W <= 105
*/
struct Compare
{
bool operator()(Item &a, Item &b)
{
double A = a.value / (1.0 * a.weight), B = b.value / (1.0 * b.weight);
return A < B;
}
};
double fractionalKnapsack(int W, Item arr[], int n)
{
priority_queue<Item, vector<Item>, Compare> pq;
for (int i = 0; i < n; ++i)
pq.push(arr[i]);
double ans = 0;
double weight = 0;
while (weight < W && pq.size())
{
auto item = pq.top();
pq.pop();
if (weight + item.weight <= W)
{
weight += item.weight;
ans += item.value;
}
else
{
double part = (W - weight) / (1.0 * item.weight);
weight = W;
ans += part * item.value;
}
}
return ans;
}