返回介绍

solution / 2100-2199 / 2144.Minimum Cost of Buying Candies With Discount / README_EN

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

2144. Minimum Cost of Buying Candies With Discount

中文文档

Description

A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free.

The customer can choose any candy to take away for free as long as the cost of the chosen candy is less than or equal to the minimum cost of the two candies bought.

  • For example, if there are 4 candies with costs 1, 2, 3, and 4, and the customer buys candies with costs 2 and 3, they can take the candy with cost 1 for free, but not the candy with cost 4.

Given a 0-indexed integer array cost, where cost[i] denotes the cost of the ith candy, return _the minimum cost of buying all the candies_.

 

Example 1:

Input: cost = [1,2,3]
Output: 5
Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies.
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.

Example 2:

Input: cost = [6,5,7,9,2,2]
Output: 23
Explanation: The way in which we can get the minimum cost is described below:
- Buy candies with costs 9 and 7
- Take the candy with cost 6 for free
- We buy candies with costs 5 and 2
- Take the last remaining candy with cost 2 for free
Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.

Example 3:

Input: cost = [5,5]
Output: 10
Explanation: Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
Hence, the minimum cost to buy all candies is 5 + 5 = 10.

 

Constraints:

  • 1 <= cost.length <= 100
  • 1 <= cost[i] <= 100

Solutions

Solution 1

class Solution:
  def minimumCost(self, cost: List[int]) -> int:
    cost.sort(reverse=True)
    return sum(cost) - sum(cost[2::3])
class Solution {
  public int minimumCost(int[] cost) {
    Arrays.sort(cost);
    int ans = 0;
    for (int i = cost.length - 1; i >= 0; i -= 3) {
      ans += cost[i];
      if (i > 0) {
        ans += cost[i - 1];
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minimumCost(vector<int>& cost) {
    sort(cost.rbegin(), cost.rend());
    int ans = 0;
    for (int i = 0; i < cost.size(); i += 3) {
      ans += cost[i];
      if (i < cost.size() - 1) {
        ans += cost[i + 1];
      }
    }
    return ans;
  }
};
func minimumCost(cost []int) (ans int) {
  sort.Ints(cost)
  for i := len(cost) - 1; i >= 0; i -= 3 {
    ans += cost[i]
    if i > 0 {
      ans += cost[i-1]
    }
  }
  return
}
function minimumCost(cost: number[]): number {
  cost.sort((a, b) => a - b);
  let ans = 0;
  for (let i = cost.length - 1; i >= 0; i -= 3) {
    ans += cost[i];
    if (i) {
      ans += cost[i - 1];
    }
  }
  return ans;
}

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

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

发布评论

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