-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathFractionalKnapsack.cpp
41 lines (31 loc) · 1002 Bytes
/
FractionalKnapsack.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
// https://practice.geeksforgeeks.org/problems/fractional-knapsack-1587115620/1
class Solution
{
public:
//Function to get the maximum total value in the knapsack.
static bool cmp(pair<double,Item> a, pair<double,Item> b){
return a.first>b.first;
}
double fractionalKnapsack(int W, Item arr[], int n)
{
// Your code here
vector<pair<double, Item>> k;
for(int i=0; i<n; i++){
double perUnitValue= (1.0*arr[i].value)/arr[i].weight;
k.push_back(make_pair(perUnitValue, arr[i]));
}
sort(k.begin(), k.end(), cmp);
double maxValue= 0.0;
for(int i=0; i<n; i++){
if(k[i].second.weight>W){
maxValue+= W*k[i].first;
W= 0;
}
else{
maxValue+= k[i].second.value;
W-= k[i].second.weight;
}
}
return maxValue;
}
};