返回介绍

solution / 2500-2599 / 2548.Maximum Price to Fill a Bag / README_EN

发布于 2024-06-17 01:03:04 字数 5128 浏览 0 评论 0 收藏 0

2548. Maximum Price to Fill a Bag

中文文档

Description

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 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.

 

Example 1:

Input: items = [[50,1],[10,8]], capacity = 5
Output: 55.00000
Explanation: 
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.0 is the maximum total price that we can achieve.

Example 2:

Input: items = [[100,30]], capacity = 50
Output: -1.00000
Explanation: It is impossible to fill a bag with the given item.

 

Constraints:

  • 1 <= items.length <= 105
  • items[i].length == 2
  • 1 <= pricei, weighti <= 104
  • 1 <= capacity <= 109

Solutions

Solution 1: Greedy + Sorting

We sort the items in descending order by unit price, and then take out the items one by one until the backpack is full.

If the backpack is not full in the end, return $-1$, otherwise return the total price.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of items.

class Solution:
  def maxPrice(self, items: List[List[int]], capacity: int) -> float:
    ans = 0
    for p, w in sorted(items, key=lambda x: x[1] / x[0]):
      v = min(w, capacity)
      ans += v / w * p
      capacity -= v
    return -1 if capacity else ans
class Solution {
  public double maxPrice(int[][] items, int capacity) {
    Arrays.sort(items, (a, b) -> a[1] * b[0] - a[0] * b[1]);
    double ans = 0;
    for (var e : items) {
      int p = e[0], w = e[1];
      int v = Math.min(w, capacity);
      ans += v * 1.0 / w * p;
      capacity -= v;
    }
    return capacity > 0 ? -1 : ans;
  }
}
class Solution {
public:
  double maxPrice(vector<vector<int>>& items, int capacity) {
    sort(items.begin(), items.end(), [&](const auto& a, const auto& b) { return a[1] * b[0] < a[0] * b[1]; });
    double ans = 0;
    for (auto& e : items) {
      int p = e[0], w = e[1];
      int v = min(w, capacity);
      ans += v * 1.0 / w * p;
      capacity -= v;
    }
    return capacity > 0 ? -1 : ans;
  }
};
func maxPrice(items [][]int, capacity int) (ans float64) {
  sort.Slice(items, func(i, j int) bool { return items[i][1]*items[j][0] < items[i][0]*items[j][1] })
  for _, e := range items {
    p, w := e[0], e[1]
    v := min(w, capacity)
    ans += float64(v) / float64(w) * float64(p)
    capacity -= v
  }
  if capacity > 0 {
    return -1
  }
  return
}
function maxPrice(items: number[][], capacity: number): number {
  items.sort((a, b) => a[1] * b[0] - a[0] * b[1]);
  let ans = 0;
  for (const [p, w] of items) {
    const v = Math.min(w, capacity);
    ans += (v / w) * p;
    capacity -= v;
  }
  return capacity ? -1 : ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文