-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionalKnapsackProblem.cpp
More file actions
31 lines (28 loc) · 946 Bytes
/
Copy pathFractionalKnapsackProblem.cpp
File metadata and controls
31 lines (28 loc) · 946 Bytes
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
// Q46 https://www.codingninjas.com/codestudio/problems/975286?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website
// Time: O(nlog(n))
// Space: O(1)
#include<algorithm>
bool comp(pair<int, int> p1,pair<int, int>p2){
double v1=(double) p1.second/(double)p1.first;
double v2=(double) p2.second/(double)p2.first;
return v2<v1;
}
double maximumValue (vector<pair<int, int>>& items, int n, int w){
sort(items.begin(),items.end(),comp);
// for(auto i:items)
// cout<<i.first<<" "<<i.second<<" "<<(double)i.second/i.first<<endl;
double cw=0, v=0.0;
for(int i=0;i<n;i++){
if(cw+items[i].first<=w){
cw+=items[i].first;
v+=items[i].second;
// cout<<items[i].second<<" ";
}
else{
double remain=w-cw;
v+=((double)items[i].second/(double)items[i].first)*(remain);
break;
}
}
return v;
}