-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3074-apple-redistribution-into-boxes.js
More file actions
38 lines (35 loc) · 1.03 KB
/
3074-apple-redistribution-into-boxes.js
File metadata and controls
38 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 3074. Apple Redistribution into Boxes
* https://leetcode.com/problems/apple-redistribution-into-boxes/
* Difficulty: Easy
*
* You are given an array apple of size n and an array capacity of size m.
*
* There are n packs where the ith pack contains apple[i] apples. There are m boxes as well,
* and the ith box has a capacity of capacity[i] apples.
*
* Return the minimum number of boxes you need to select to redistribute these n packs of
* apples into boxes.
*
* Note that, apples from the same pack can be distributed into different boxes.
*/
/**
* @param {number[]} apple
* @param {number[]} capacity
* @return {number}
*/
var minimumBoxes = function(apple, capacity) {
const totalApples = apple.reduce((sum, num) => sum + num, 0);
const sortedCapacities = capacity.sort((a, b) => b - a);
let currentCapacity = 0;
let result = 0;
for (const box of sortedCapacities) {
if (currentCapacity < totalApples) {
currentCapacity += box;
result++;
} else {
break;
}
}
return result;
};