-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractional Knapsack.java
More file actions
39 lines (39 loc) · 1.15 KB
/
Fractional Knapsack.java
File metadata and controls
39 lines (39 loc) · 1.15 KB
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
class Item{
int value;
int weight;
Item(int value, int weight){
this.value = value;
this.weight = weight;
}
}
class Solution {
double fractionalKnapsack(int[] values, int[] weights, int W) {
int n = values.length;
Item[] items = new Item[n];
for(int i = 0; i < n; i++){
items[i] = new Item(values[i], weights[i]);
}
// Sort in Descending Order
Arrays.sort(items, (a, b) -> {
double ratio1 = (double) a.value / a.weight;
double ratio2 = (double) b.value / b.weight;
return (Double.compare(ratio2, ratio1));
});
// compute the maximum value
double maxValue = 0.0;
for(int i = 0; i < n; i++){
if(W == 0){
break; // Bag Full - Break the loop
}
if(items[i].weight <= W){
maxValue = maxValue + items[i].value;
W = W - items[i].weight;
}
else{
maxValue = maxValue + (double) (items[i].value * W) / items[i].weight;
W = 0;
}
}
return maxValue;
}
}