Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 62d047b

Browse files
committed
Add solution #3074
1 parent e4ebac1 commit 62d047b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@
21572157
3069|[Distribute Elements Into Two Arrays I](./solutions/3069-distribute-elements-into-two-arrays-i.js)|Easy|
21582158
3070|[Count Submatrices with Top-Left Element and Sum Less Than k](./solutions/3070-count-submatrices-with-top-left-element-and-sum-less-than-k.js)|Medium|
21592159
3071|[Minimum Operations to Write the Letter Y on a Grid](./solutions/3071-minimum-operations-to-write-the-letter-y-on-a-grid.js)|Medium|
2160+
3074|[Apple Redistribution into Boxes](./solutions/3074-apple-redistribution-into-boxes.js)|Easy|
21602161
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21612162
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21622163
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 3074. Apple Redistribution into Boxes
3+
* https://leetcode.com/problems/apple-redistribution-into-boxes/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array apple of size n and an array capacity of size m.
7+
*
8+
* There are n packs where the ith pack contains apple[i] apples. There are m boxes as well,
9+
* and the ith box has a capacity of capacity[i] apples.
10+
*
11+
* Return the minimum number of boxes you need to select to redistribute these n packs of
12+
* apples into boxes.
13+
*
14+
* Note that, apples from the same pack can be distributed into different boxes.
15+
*/
16+
17+
/**
18+
* @param {number[]} apple
19+
* @param {number[]} capacity
20+
* @return {number}
21+
*/
22+
var minimumBoxes = function(apple, capacity) {
23+
const totalApples = apple.reduce((sum, num) => sum + num, 0);
24+
const sortedCapacities = capacity.sort((a, b) => b - a);
25+
let currentCapacity = 0;
26+
let result = 0;
27+
28+
for (const box of sortedCapacities) {
29+
if (currentCapacity < totalApples) {
30+
currentCapacity += box;
31+
result++;
32+
} else {
33+
break;
34+
}
35+
}
36+
37+
return result;
38+
};

0 commit comments

Comments
 (0)