-
Notifications
You must be signed in to change notification settings - Fork 5
/
FractionalKnapsack.java
75 lines (63 loc) · 2.14 KB
/
FractionalKnapsack.java
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
/*https://practice.geeksforgeeks.org/problems/fractional-knapsack-1587115620/1*/
class ItemExtended extends Item implements Comparable<ItemExtended> {
double profitPerWeight;
ItemExtended(int x, int y, double z) {
super(x,y);
this.profitPerWeight = z;
}
public int getValue() { return super.value; }
public int getWeight() { return super.weight; }
@Override
public String toString() { return ""; }
@Override
public int compareTo(ItemExtended i) {
if (this.profitPerWeight < i.profitPerWeight) return 1;
if (this.profitPerWeight > i.profitPerWeight) return -1;
return 0;
}
}
class Solution
{
//Function to get the maximum total value in the knapsack.
double fractionalKnapsack(int W, Item arr[], int n)
{
// Your code here
double profit = 0;
//rebuild the item array along with the profit per weight
ItemExtended[] items = new ItemExtended[arr.length];
for (int i = 0; i < arr.length; ++i)
{
double profitPerWeight = (double)arr[i].value/(double)arr[i].weight;
items[i] = new ItemExtended(arr[i].value,arr[i].weight,profitPerWeight);
}
//add everything to the minheap
PriorityQueue<ItemExtended> pq = new PriorityQueue<ItemExtended>();
for (int i = 0; i < items.length; ++i)
pq.add(items[i]);
//for each item in the minheap
ItemExtended item = null;
for (int i = 0; i < items.length; ++i)
{
//till more items can be added
if (W > 0) {
//remove the root
item = pq.poll();
//if it fits fully, add it
if (item.getWeight() <= W)
{
W -= item.getWeight();
profit += item.getValue();
}
//otehrwise add a part of it
else
{
profit += W*item.profitPerWeight;
W = 0;
}
}
//otherwise break
else break;
}
return profit;
}
}