commit 6da1a3a48cb3ac0bf2e4cacbf1ecaef106dee100
parent fcc867f0b2218fb411bcc4324e5bdb545ccaabb1
Author: Jack Sangdahl <jack@pngu.org>
Date: Sun, 21 Jun 2026 14:10:29 -0600
1833. maximum ice cream bars
Diffstat:
2 files changed, 52 insertions(+), 18 deletions(-)
diff --git a/max_ice_cream_bars.c b/max_ice_cream_bars.c
@@ -1,6 +1,5 @@
// 1833. Maximum Ice Cream Bars
-#include <stdio.h>
-#include <stdlib.h>
+#include "leetcode.h"
/*
* it is a sweltering summer day, and a boy wants to buy some ice cream bars. at
@@ -12,26 +11,38 @@
* 'coins' coins. you must solve the problem by counting sort.
*/
-int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
-
-int maxIceCream(int *costs, int costs_size, int coins) {
- int n = costs_size;
- qsort(costs, n, sizeof(int), cmp);
- int ans = 0, sum = 0;
- for (int i = 0; i < n; i++) {
- sum += costs[i];
- if (sum <= coins)
- ans++;
- else
+int maxIceCream(int *costs, int costsSize, int coins) {
+ int max_cost = 0;
+ for (int i = 0; i < costsSize; i++)
+ max_cost = fmax(max_cost, costs[i]);
+ int *freq = (int *)calloc(max_cost + 1, sizeof(int));
+ for (int i = 0; i < costsSize; i++)
+ freq[costs[i]]++;
+ int ans = 0;
+ for (int cost = 1; cost <= max_cost; cost++) {
+ if (!freq[cost])
+ continue;
+ int can_buy = freq[cost] < coins / cost ? freq[cost] : coins / cost;
+ ans += can_buy;
+ coins -= can_buy * cost;
+ if (coins < cost)
break;
}
+ free(freq);
return ans;
}
int main() {
- int c1[] = {1, 3, 2, 4, 1}, c2[] = {10, 6, 8, 7, 7, 8},
- c3[] = {1, 6, 3, 1, 2, 5};
- printf("%d\n", maxIceCream(c1, 5, 7)); // expect: 4
- printf("%d\n", maxIceCream(c2, 6, 5)); // expect: 0
- printf("%d\n", maxIceCream(c3, 6, 20)); // expect: 6
+ int c1[] = {1, 3, 2, 4, 1};
+ int c2[] = {10, 6, 8, 7, 7, 8};
+ int c3[] = {1, 6, 3, 1, 2, 5};
+ int r1 = maxIceCream(c1, ARRAY_SIZE(c1), 7);
+ int r2 = maxIceCream(c2, ARRAY_SIZE(c2), 5);
+ int r3 = maxIceCream(c3, ARRAY_SIZE(c3), 20);
+ printf("%d\n", r1);
+ assert(r1 == 4);
+ printf("%d\n", r2);
+ assert(r2 == 0);
+ printf("%d\n", r3);
+ assert(r3 == 6);
}
diff --git a/max_ice_cream_bars.py b/max_ice_cream_bars.py
@@ -0,0 +1,23 @@
+# 1833. Maximum Ice Cream Bars
+
+"""
+it is a sweltering summer day, and a boy wants to buy some ice cream bars. at
+the store, there are 'n' ice cream bars. you are given an array 'costs' of
+length 'n' where 'costs[i]' is the price of the i'th ice cream bar in coins.
+the boy initially has 'coins' coins to spend and he wants to buy as many ice
+cream bars as possible. note, the boy can buy the ice cream bars in any
+order. return the maximum number of ice cream bars the boy can buy with
+'coins' coins. you must solve the problem by counting sort.
+"""
+
+
+class Solution:
+ def maxIceCream(self, costs: List[int], coins: int) -> int:
+ return sum(1 for i in sorted(costs) if (coins := coins - i) >= 0)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxIceCream(costs=[1, 3, 2, 4, 1], coins=7))
+ print(obj.maxIceCream(costs=[10, 6, 8, 7, 7, 8], coins=5))
+ print(obj.maxIceCream(costs=[1, 6, 3, 1, 2, 5], coins=20))