3074 apple redistribution:
```
/**
* @param {number[]} apple
* @param {number[]} capacity
* @return {number}
*/
var minimumBoxes = function(apple, capacity) {
let sum = apple.reduce((partialSum, a) => partialSum + a, 0);
let minBoxCount = 0;
capacity.sort((a,b)=> a-b);
while(sum > 0){
sum -= capacity.pop();
minBoxCount++;
}
return minBoxCount;
};
```
Algorithm description:
My intuition here was that the sum of the values in the apple array was more helpful than the array itself. After getting the sum, you can find the mind boxes by popping the largest boxes off the array and subtracting them from the sum until you reach 0 or less. Every time we pop a box, we increment 1 to the minimum box count, so that we can return the count once we run out of apples.
Time Complexity:
The reduce call in the first call is O(n) where n is the number of apple packs.
The sort call time complexity is O(mlogm) where m is the number of boxes.
The while loop can iterate up to k times depending on the values k in the apple array.
Overall the worst case time complexity is O(n+mlogm+k).
Space Complexity:
The space complexity is O(1) because we are not creating any new arrays or non-constant data structures to solve this.
Any feedback is welcome!