mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
add leetcode Maximum Bags With Full Capacity of Rocks (#1196)
* add leetcode Maximum Bags With Full Capacity of Rocks * Update leetcode/src/2279.c Co-authored-by: David Leal <halfpacho@gmail.com> * Update leetcode/src/2279.c Co-authored-by: David Leal <halfpacho@gmail.com> * Update leetcode/src/2279.c Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
b98296e02f
commit
17b7131873
@ -129,6 +129,7 @@
|
||||
| 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium |
|
||||
| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [C](./src/2256.c) | Medium |
|
||||
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium |
|
||||
| 2279 | [Maximum Bags With Full Capacity of Rocks](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/) | [C](./src/2279.c) | Medium |
|
||||
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |
|
||||
| 2482 | [Difference Between Ones and Zeros in Row and Column](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/description/) | [C](./src/2482.c) | Medium |
|
||||
| 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array/description/) | [C](./src/2501.c) | Medium |
|
||||
|
29
leetcode/src/2279.c
Normal file
29
leetcode/src/2279.c
Normal file
@ -0,0 +1,29 @@
|
||||
int compare(const int* i, const int* j)
|
||||
{
|
||||
return *i - *j;
|
||||
}
|
||||
|
||||
// Sorting.
|
||||
// Runtime: O(n*log(n))
|
||||
// Space: O(n)
|
||||
int maximumBags(int* capacity, int capacitySize, int* rocks, int rocksSize, int additionalRocks) {
|
||||
int* capacityLeft = malloc(capacitySize * sizeof(int));
|
||||
for (int i = 0; i < capacitySize; i++) {
|
||||
capacityLeft[i] = capacity[i] - rocks[i];
|
||||
}
|
||||
|
||||
qsort(capacityLeft, capacitySize, sizeof (int), (int(*) (const void*, const void*)) compare);
|
||||
|
||||
int bags = 0;
|
||||
for (int i = 0; i < capacitySize; i++) {
|
||||
if (additionalRocks < capacityLeft[i]){
|
||||
break;
|
||||
}
|
||||
|
||||
additionalRocks -= capacityLeft[i];
|
||||
bags++;
|
||||
}
|
||||
|
||||
free(capacityLeft);
|
||||
return bags;
|
||||
}
|
Loading…
Reference in New Issue
Block a user