diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index f37d64fc..972d8143 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -122,6 +122,7 @@ | 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy | | 1838 | [Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) | [C](./src/1838.c) | Medium | | 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy | +| 1833 | [Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [C](./src/1833.c) | Medium | | 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium | | 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [C](./src/2095.c) | Medium | | 2125 | [Number of Laser Beams in a Bank](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/) | [C](./src/2125.c) | Medium | diff --git a/leetcode/src/1833.c b/leetcode/src/1833.c new file mode 100644 index 00000000..e77d8a29 --- /dev/null +++ b/leetcode/src/1833.c @@ -0,0 +1,24 @@ +int compare(const void* i, const void* j) +{ + return *((int*)i) - *((int*)j); +} + +// Greedy + sorting +// Runtime: O(n*log(n)) +// Space: O(1) +int maxIceCream(int* costs, int costsSize, int coins){ + qsort(costs, costsSize, sizeof(int), compare); + + int result = 0; + int leftCoins = coins; + for (int i = 0; i < costsSize; i++){ + if (costs[i] > leftCoins){ + break; + } + + leftCoins -= costs[i]; + result++; + } + + return result; +}