mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
feat: add Maximum Ice Cream Bars (#1197)
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
17b7131873
commit
a680c83b83
@ -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 |
|
||||
|
24
leetcode/src/1833.c
Normal file
24
leetcode/src/1833.c
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user