返回介绍

solution / 1600-1699 / 1648.Sell Diminishing-Valued Colored Balls / README_EN

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

1648. Sell Diminishing-Valued Colored Balls

中文文档

Description

You have an inventory of different colored balls, and there is a customer that wants orders balls of any color.

The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your inventory. For example, if you own 6 yellow balls, the customer would pay 6 for the first yellow ball. After the transaction, there are only 5 yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls decreases as you sell more to the customer).

You are given an integer array, inventory, where inventory[i] represents the number of balls of the ith color that you initially own. You are also given an integer orders, which represents the total number of balls that the customer wants. You can sell the balls in any order.

Return _the maximum total value that you can attain after selling _orders_ colored balls_. As the answer may be too large, return it modulo 109 + 7.

 

Example 1:

Input: inventory = [2,5], orders = 4
Output: 14
Explanation: Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).
The maximum total value is 2 + 5 + 4 + 3 = 14.

Example 2:

Input: inventory = [3,5], orders = 6
Output: 19
Explanation: Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).
The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.

 

Constraints:

  • 1 <= inventory.length <= 105
  • 1 <= inventory[i] <= 109
  • 1 <= orders <= min(sum(inventory[i]), 109)

Solutions

Solution 1

class Solution:
  def maxProfit(self, inventory: List[int], orders: int) -> int:
    inventory.sort(reverse=True)
    mod = 10**9 + 7
    ans = i = 0
    n = len(inventory)
    while orders > 0:
      while i < n and inventory[i] >= inventory[0]:
        i += 1
      nxt = 0
      if i < n:
        nxt = inventory[i]
      cnt = i
      x = inventory[0] - nxt
      tot = cnt * x
      if tot > orders:
        decr = orders // cnt
        a1, an = inventory[0] - decr + 1, inventory[0]
        ans += (a1 + an) * decr // 2 * cnt
        ans += (inventory[0] - decr) * (orders % cnt)
      else:
        a1, an = nxt + 1, inventory[0]
        ans += (a1 + an) * x // 2 * cnt
        inventory[0] = nxt
      orders -= tot
      ans %= mod
    return ans
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int maxProfit(int[] inventory, int orders) {
    Arrays.sort(inventory);
    int n = inventory.length;
    for (int i = 0, j = n - 1; i < j; ++i, --j) {
      int t = inventory[i];
      inventory[i] = inventory[j];
      inventory[j] = t;
    }
    long ans = 0;
    int i = 0;
    while (orders > 0) {
      while (i < n && inventory[i] >= inventory[0]) {
        ++i;
      }
      int nxt = i < n ? inventory[i] : 0;
      int cnt = i;
      long x = inventory[0] - nxt;
      long tot = cnt * x;
      if (tot > orders) {
        int decr = orders / cnt;
        long a1 = inventory[0] - decr + 1, an = inventory[0];
        ans += (a1 + an) * decr / 2 * cnt;
        ans += (a1 - 1) * (orders % cnt);
      } else {
        long a1 = nxt + 1, an = inventory[0];
        ans += (a1 + an) * x / 2 * cnt;
        inventory[0] = nxt;
      }
      orders -= tot;
      ans %= MOD;
    }
    return (int) ans;
  }
}
class Solution {
public:
  int maxProfit(vector<int>& inventory, int orders) {
    long ans = 0, mod = 1e9 + 7;
    int i = 0, n = inventory.size();
    sort(inventory.rbegin(), inventory.rend());
    while (orders > 0) {
      while (i < n && inventory[i] >= inventory[0]) {
        ++i;
      }
      int nxt = i < n ? inventory[i] : 0;
      int cnt = i;
      long x = inventory[0] - nxt;
      long tot = cnt * x;
      if (tot > orders) {
        int decr = orders / cnt;
        long a1 = inventory[0] - decr + 1, an = inventory[0];
        ans += (a1 + an) * decr / 2 * cnt;
        ans += (a1 - 1) * (orders % cnt);
      } else {
        long a1 = nxt + 1, an = inventory[0];
        ans += (a1 + an) * x / 2 * cnt;
        inventory[0] = nxt;
      }
      orders -= tot;
      ans %= mod;
    }
    return ans;
  }
};
func maxProfit(inventory []int, orders int) int {
  var mod int = 1e9 + 7
  i, n, ans := 0, len(inventory), 0
  sort.Ints(inventory)
  for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
    inventory[i], inventory[j] = inventory[j], inventory[i]
  }
  for orders > 0 {
    for i < n && inventory[i] >= inventory[0] {
      i++
    }
    nxt := 0
    if i < n {
      nxt = inventory[i]
    }
    cnt := i
    x := inventory[0] - nxt
    tot := cnt * x
    if tot > orders {
      decr := orders / cnt
      a1, an := inventory[0]-decr+1, inventory[0]
      ans += (a1 + an) * decr / 2 * cnt
      ans += (a1 - 1) * (orders % cnt)
    } else {
      a1, an := nxt+1, inventory[0]
      ans += (a1 + an) * x / 2 * cnt
      inventory[0] = nxt
    }
    orders -= tot
    ans %= mod
  }
  return ans
}

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

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

发布评论

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