|
| 1 | +--- |
| 2 | +id: maximum-price-to-fill-a-bag |
| 3 | +title: Maximum Price to Fill a Bag |
| 4 | +sidebar_label: 2548 Maximum Price to Fill a Bag |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Maths |
| 8 | + - sort |
| 9 | + - LeetCode |
| 10 | + - java |
| 11 | +description: "This is a solution to the Maximum Price to Fill a Bag problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +You are given a 2D integer array items where items[i] = [pricei, weighti] denotes the price and weight of the ith item, respectively. |
| 17 | + |
| 18 | +You are also given a positive integer capacity. |
| 19 | + |
| 20 | +Each item can be divided into two items with ratios part1 and part2, where part1 + part2 == 1. |
| 21 | + |
| 22 | +The weight of the first item is weighti * part1 and the price of the first item is pricei * part1. |
| 23 | +Similarly, the weight of the second item is weighti * part2 and the price of the second item is pricei * part2. |
| 24 | +Return the maximum total price to fill a bag of capacity capacity with given items. If it is impossible to fill a bag return -1. Answers within 10-5 of the actual answer will be considered accepted. |
| 25 | + |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: items = [[50,1],[10,8]], capacity = 5 |
| 33 | +Output: 55.00000 |
| 34 | +Explanation: |
| 35 | +We divide the 2nd item into two parts with part1 = 0.5 and part2 = 0.5. |
| 36 | +
|
| 37 | +``` |
| 38 | + |
| 39 | +**Example 2:** |
| 40 | + |
| 41 | +``` |
| 42 | +Input: items = [[100,30]], capacity = 50 |
| 43 | +Output: -1.00000 |
| 44 | +Explanation: It is impossible to fill a bag with the given item. |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +### Constraints |
| 50 | + |
| 51 | +- `1 <= items.length <= 105` |
| 52 | +- `items[i].length == 2` |
| 53 | +- `1 <= pricei, weighti <= 104` |
| 54 | +- `1 <= capacity <= 109` |
| 55 | + |
| 56 | +### Approach |
| 57 | + |
| 58 | +We sort the items in descending order by unit price, and then take out the items one by one until the backpack is full. |
| 59 | + |
| 60 | +If the backpack is not full in the end, return $-1$, otherwise return the total price. |
| 61 | + |
| 62 | +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of items. |
| 63 | + |
| 64 | +#### Python3 |
| 65 | + |
| 66 | +```python |
| 67 | +class Solution: |
| 68 | + def maxPrice(self, items: List[List[int]], capacity: int) -> float: |
| 69 | + ans = 0 |
| 70 | + for p, w in sorted(items, key=lambda x: x[1] / x[0]): |
| 71 | + v = min(w, capacity) |
| 72 | + ans += v / w * p |
| 73 | + capacity -= v |
| 74 | + return -1 if capacity else ans |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +#### Java |
| 79 | + |
| 80 | +```java |
| 81 | +class Solution { |
| 82 | + public double maxPrice(int[][] items, int capacity) { |
| 83 | + Arrays.sort(items, (a, b) -> a[1] * b[0] - a[0] * b[1]); |
| 84 | + double ans = 0; |
| 85 | + for (var e : items) { |
| 86 | + int p = e[0], w = e[1]; |
| 87 | + int v = Math.min(w, capacity); |
| 88 | + ans += v * 1.0 / w * p; |
| 89 | + capacity -= v; |
| 90 | + } |
| 91 | + return capacity > 0 ? -1 : ans; |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### C++ |
| 97 | + |
| 98 | +```cpp |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + double maxPrice(vector<vector<int>>& items, int capacity) { |
| 102 | + sort(items.begin(), items.end(), [&](const auto& a, const auto& b) { return a[1] * b[0] < a[0] * b[1]; }); |
| 103 | + double ans = 0; |
| 104 | + for (auto& e : items) { |
| 105 | + int p = e[0], w = e[1]; |
| 106 | + int v = min(w, capacity); |
| 107 | + ans += v * 1.0 / w * p; |
| 108 | + capacity -= v; |
| 109 | + } |
| 110 | + return capacity > 0 ? -1 : ans; |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
0 commit comments