diff --git a/3334-apple-redistribution-into-boxes/README.md b/3334-apple-redistribution-into-boxes/README.md new file mode 100644 index 0000000..09dff30 --- /dev/null +++ b/3334-apple-redistribution-into-boxes/README.md @@ -0,0 +1,35 @@ +

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.

+ +

 

+

Example 1:

+ +
+Input: apple = [1,3,2], capacity = [4,3,1,5,2]
+Output: 2
+Explanation: We will use boxes with capacities 4 and 5.
+It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
+
+ +

Example 2:

+ +
+Input: apple = [5,5,5], capacity = [2,4,2,7]
+Output: 4
+Explanation: We will need to use all the boxes.
+
+ +

 

+

Constraints:

+ + diff --git a/3334-apple-redistribution-into-boxes/solution.go b/3334-apple-redistribution-into-boxes/solution.go new file mode 100644 index 0000000..20e2ed8 --- /dev/null +++ b/3334-apple-redistribution-into-boxes/solution.go @@ -0,0 +1,22 @@ +func minimumBoxes(apple []int, capacity []int) int { + sum := 0 + sum2 := 0 + + for _, a := range apple { + sum += a + } + + sort.Slice(capacity, func(i, j int) bool { + return capacity[i] > capacity[j] + }) + + for i, c := range capacity { + sum2 += c + + if sum2 >= sum { + return i + 1 + } + } + + return -1 +}