leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit d269aa5065ed6b14a7eae2415c463f34eda0831a
parent 4eb955fc2d278c9acbb3071f3b3866b887f356c7
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 23 Dec 2025 22:21:52 -0700

3074. apple redistribution into boxes

Diffstat:
Aapple_redistribution_boxes.c | 32++++++++++++++++++++++++++++++++
Aapple_redistribution_boxes.py | 31+++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/apple_redistribution_boxes.c b/apple_redistribution_boxes.c @@ -0,0 +1,32 @@ +// 3074. Apple Redistribution into Boxes +#include "leetcode.h" + +/* + * you are given an array 'apple' of size 'n' and an array 'capacity' of size + * 'm'. there are 'n' packs where the i'th pack contains 'apple[i]' apples. + * there are 'm' boxes as well, and the i'th box has a capacity of 'capacity[i]' + * apples. return the minimum number of boxes you need to redistribute these 'n' + * packs of apples into boxes. note that apples from the same pack can be + * distributed into different boxes. + */ + +int cmp(const void *a, const void *b) { return (*(int *)b - *(int *)a); } + +int minimumBoxes(int *apple, int appleSize, int *capacity, int capacitySize) { + int total = 0, ans = 0; + for (int i = 0; i < appleSize; i++) + total += apple[i]; + qsort(capacity, capacitySize, sizeof(int), cmp); + while (total > 0) + total -= capacity[ans++]; + return ans; +} + +int main() { + int a1[] = {1, 3, 2}, c1[] = {4, 3, 1, 5, 2}; + int a2[] = {5, 5, 5}, c2[] = {2, 4, 2, 7}; + printf("%d\n", + minimumBoxes(a1, ARRAY_SIZE(a1), c1, ARRAY_SIZE(c1))); // expect: 2 + printf("%d\n", + minimumBoxes(a2, ARRAY_SIZE(a2), c2, ARRAY_SIZE(c2))); // expect: 4 +} diff --git a/apple_redistribution_boxes.py b/apple_redistribution_boxes.py @@ -0,0 +1,31 @@ +# 3074. Apple Redistribution into Boxes + +""" +you are given an array 'apple' of size 'n' and an array 'capacity' of size +'m'. there are 'n' packs where the i'th pack contains 'apple[i]' apples. +there are 'm' boxes as well, and the i'th box has a capacity of 'capacity[i]' +apples. return the minimum number of boxes you need to redistribute these 'n' +packs of apples into boxes. note that apples from the same pack can be +distributed into different boxes. +""" + + +class Solution(object): + def minimumBoxes(self, apple, capacity): + """ + :type apple: List[int] + :type capacity: List[int] + :rtype: int + """ + total, ans = sum(apple), 0 + capacity.sort(reverse=True) + while total > 0: + total -= capacity[ans] + ans += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumBoxes(apple=[1, 3, 2], capacity=[4, 3, 1, 5, 2])) + print(obj.minimumBoxes(apple=[5, 5, 5], capacity=[2, 4, 2, 7]))