You are given a 2D integer array items where items[i] = [pricei, weighti]
denotes the price and weight of the ith item, respectively.
You are also given a positive integer capacity.
Each item can be divided into two items with ratios part1 and part2, where
part1 + part2 == 1.
The weight of the first item is weighti * part1 and the price of the first item is pricei * part1.
Similarly, the weight of the second item is weighti * part2 and the price of the second item is pricei * part2.
Return the maximum total price to fill a bag of capacitycapacitywith given items. If it is impossible to fill a bag return -1. Answers within 10-5 of the actual answer will be considered accepted.
Input: items =[[50,1],[10,8]], capacity =5Output: 55.00000Explanation:
We divide the 2nd item into two parts with part1 =0.5 and part2 =0.5.The price and weight of the 1st item are 5,4. And similarly, the price and the weight of the 2nd item are 5,4.The array items after operation becomes [[50,1],[5,4],[5,4]].To fill a bag with capacity 5 we take the 1st element with a price of 50 and the 2nd element with a price of 5.It can be proved that 55.0is the maximum total price that we can achieve.
Example 2:
1
2
3
Input: items =[[100,30]], capacity =50Output: -1.00000Explanation: It is impossible to fill a bag with the given item.
To maximize the total price, always take as much as possible from the item with the highest price per unit weight. This is the classic fractional knapsack problem, where we can take fractions of items.
classSolution {
publicdoublemaximumPrice(int[][] items, int capacity) {
int n = items.length;
double[][] v =newdouble[n][2];
for (int i = 0; i < n; ++i) v[i]=newdouble[]{(double)items[i][0]/items[i][1], i};
Arrays.sort(v, (a, b) -> Double.compare(b[0], a[0]));
double ans = 0;
for (double[] p : v) {
int idx = (int)p[1];
int w = items[idx][1], pr = items[idx][0];
if (capacity >= w) {
ans += pr;
capacity -= w;
} else {
ans += p[0]* capacity;
capacity = 0;
break;
}
}
return capacity == 0 ? ans : -1;
}
}
classSolution {
funmaximumPrice(items: Array<IntArray>, capacity: Int): Double {
val v = items.mapIndexed { i, it-> Pair(it[0].toDouble()/it[1], i) }.sortedByDescending { it.first }
var cap = capacity
var ans = 0.0for ((ratio, idx) in v) {
val w = items[idx][1]
val p = items[idx][0]
if (cap >= w) {
ans += p
cap -= w
} else {
ans += ratio * cap
cap = 0break }
}
returnif (cap ==0) ans else -1.0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmaximumPrice(self, items: list[list[int]], capacity: int) -> float:
v = sorted([(p/w, p, w) for p, w in items], reverse=True)
ans =0.0for ratio, p, w in v:
if capacity >= w:
ans += p
capacity -= w
else:
ans += ratio * capacity
capacity =0breakreturn ans if capacity ==0else-1.0
impl Solution {
pubfnmaximum_price(items: Vec<Vec<i32>>, mut capacity: i32) -> f64 {
letmut v: Vec<(f64, usize)>= items.iter().enumerate().map(|(i, it)| ((it[0] asf64)/(it[1] asf64), i)).collect();
v.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());
letmut ans =0.0;
for (ratio, idx) in v {
let w = items[idx][1];
let p = items[idx][0];
if capacity >= w {
ans += p asf64;
capacity -= w;
} else {
ans += ratio * capacity asf64;
capacity =0;
break;
}
}
if capacity ==0 { ans } else { -1.0 }
}
}