|
| 1 | +/** |
| 2 | + * [2144] Minimum Cost of Buying Candies With Discount |
| 3 | + * |
| 4 | + * A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free. |
| 5 | + * 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. |
| 6 | + * |
| 7 | + * 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. |
| 8 | + * |
| 9 | + * Given a 0-indexed integer array cost, where cost[i] denotes the cost of the i^th candy, return the minimum cost of buying all the candies. |
| 10 | + * |
| 11 | + * Example 1: |
| 12 | + * |
| 13 | + * Input: cost = [1,2,3] |
| 14 | + * Output: 5 |
| 15 | + * Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free. |
| 16 | + * The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies. |
| 17 | + * Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free. |
| 18 | + * The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies. |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * Input: cost = [6,5,7,9,2,2] |
| 23 | + * Output: 23 |
| 24 | + * Explanation: The way in which we can get the minimum cost is described below: |
| 25 | + * - Buy candies with costs 9 and 7 |
| 26 | + * - Take the candy with cost 6 for free |
| 27 | + * - We buy candies with costs 5 and 2 |
| 28 | + * - Take the last remaining candy with cost 2 for free |
| 29 | + * Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23. |
| 30 | + * |
| 31 | + * Example 3: |
| 32 | + * |
| 33 | + * Input: cost = [5,5] |
| 34 | + * Output: 10 |
| 35 | + * Explanation: Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free. |
| 36 | + * Hence, the minimum cost to buy all candies is 5 + 5 = 10. |
| 37 | + * |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * |
| 41 | + * 1 <= cost.length <= 100 |
| 42 | + * 1 <= cost[i] <= 100 |
| 43 | + * |
| 44 | + */ |
| 45 | +pub struct Solution {} |
| 46 | + |
| 47 | +// problem: https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/ |
| 48 | +// discuss: https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/discuss/?currentPage=1&orderBy=most_votes&query= |
| 49 | + |
| 50 | +// submission codes start here |
| 51 | + |
| 52 | +impl Solution { |
| 53 | + pub fn minimum_cost(cost: Vec<i32>) -> i32 { |
| 54 | + let mut cost = cost; |
| 55 | + |
| 56 | + cost.sort_unstable(); |
| 57 | + |
| 58 | + let mut candy_counter = 0; |
| 59 | + let mut minimum_cost = 0; |
| 60 | + |
| 61 | + while let Some(candy_cost) = cost.pop() { |
| 62 | + if candy_counter == 2 { |
| 63 | + candy_counter = 0; |
| 64 | + continue; |
| 65 | + } |
| 66 | + minimum_cost += candy_cost; |
| 67 | + candy_counter += 1; |
| 68 | + } |
| 69 | + |
| 70 | + minimum_cost |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// submission codes end |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::*; |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_2144_example_1() { |
| 82 | + let cost = vec![1, 2, 3]; |
| 83 | + |
| 84 | + let result = 5; |
| 85 | + |
| 86 | + assert_eq!(Solution::minimum_cost(cost), result); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_2144_example_2() { |
| 91 | + let cost = vec![6, 5, 7, 9, 2, 2]; |
| 92 | + |
| 93 | + let result = 23; |
| 94 | + |
| 95 | + assert_eq!(Solution::minimum_cost(cost), result); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_2144_example_3() { |
| 100 | + let cost = vec![5, 5]; |
| 101 | + |
| 102 | + let result = 10; |
| 103 | + |
| 104 | + assert_eq!(Solution::minimum_cost(cost), result); |
| 105 | + } |
| 106 | +} |
0 commit comments