commit 3f66df1e6f1e76eb7ecf866df0782125253e8335
parent df8bfcbc736c9cfcdef11294c244652c0d56ad57
Author: Jack Sangdahl <jack@pngu.org>
Date: Mon, 1 Jun 2026 08:09:50 -0600
2144. minimum cost of buying candies with discount
Diffstat:
2 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/min_cost_buy_candles.c b/min_cost_buy_candles.c
@@ -0,0 +1,35 @@
+// 2144. Minimum Cost of Buying Candies With Discount
+#include "leetcode.h"
+
+/*
+ * 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. 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.
+ */
+
+int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
+
+int minimumCost(int *cost, int costSize) {
+ qsort(cost, costSize, sizeof(int), cmp);
+ int ans = 0;
+ for (int i = 0; i < costSize; ++i)
+ if (i % 3 != 2)
+ ans += cost[i];
+ return ans;
+}
+
+int main() {
+ int c1[] = {1, 2, 3}, c2[] = {6, 5, 7, 9, 2, 2}, c3[] = {5, 5};
+ int r1 = minimumCost(c1, ARRAY_SIZE(c1));
+ int r2 = minimumCost(c2, ARRAY_SIZE(c2));
+ int r3 = minimumCost(c3, ARRAY_SIZE(c3));
+ printf("%d\n", r1);
+ assert(r1 == 5);
+ printf("%d\n", r2);
+ assert(r2 == 23);
+ printf("%d\n", r3);
+ assert(r3 == 10);
+}
diff --git a/min_cost_buy_candles.py b/min_cost_buy_candles.py
@@ -0,0 +1,23 @@
+# 2144. 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. 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.
+"""
+
+
+class Solution:
+ minimumCost = lambda self, cost: sum(
+ c for i, c in enumerate(sorted(cost, reverse=True)) if i % 3 != 2
+ )
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minimumCost(cost=[1, 2, 3]))
+ print(obj.minimumCost(cost=[6, 5, 7, 9, 2, 2]))
+ print(obj.minimumCost(cost=[5, 5]))