2144. Minimum Cost of Buying Candies With Discount LeetCode Solution

In this guide, you will get 2144. Minimum Cost of Buying Candies With Discount LeetCode Solution with the best time and space complexity. The solution to Minimum Cost of Buying Candies With Discount problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Cost of Buying Candies With Discount solution in C++
  4. Minimum Cost of Buying Candies With Discount solution in Java
  5. Minimum Cost of Buying Candies With Discount solution in Python
  6. Additional Resources
2144. Minimum Cost of Buying Candies With Discount LeetCode Solution image

Problem Statement of Minimum Cost of Buying Candies With Discount

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.

See also  3296. Minimum Number of Seconds to Make Mountain Height Zero LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

2144. Minimum Cost of Buying Candies With Discount LeetCode Solution in C++

class Solution {
 public:
  int minimumCost(vector<int>& cost) {
    int ans = 0;

    ranges::sort(cost, greater<>());

    for (int i = 0; i < cost.size(); ++i)
      if (i % 3 != 2)
        ans += cost[i];

    return ans;
  }
};
/* code provided by PROGIEZ */

2144. Minimum Cost of Buying Candies With Discount LeetCode Solution in Java

class Solution {
  public int minimumCost(int[] cost) {
    int ans = 0;

    cost = Arrays.stream(cost)
               .boxed()
               .sorted(Collections.reverseOrder())
               .mapToInt(Integer::intValue)
               .toArray();

    for (int i = 0; i < cost.length; ++i)
      if (i % 3 != 2)
        ans += cost[i];

    return ans;
  }
}
// code provided by PROGIEZ

2144. Minimum Cost of Buying Candies With Discount LeetCode Solution in Python

class Solution:
  def minimumCost(self, cost: list[int]) -> int:
    return sum(cost) - sum(sorted(cost)[-3::-3])
# code by PROGIEZ

Additional Resources

See also  855. Exam Room LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.