-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 2.7 MB (1…
…8.60%)
- Loading branch information
1 parent
d70a301
commit a638ac7
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<p>You are given an array <code>apple</code> of size <code>n</code> and an array <code>capacity</code> of size <code>m</code>.</p> | ||
|
||
<p>There are <code>n</code> packs where the <code>i<sup>th</sup></code> pack contains <code>apple[i]</code> apples. There are <code>m</code> boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of <code>capacity[i]</code> apples.</p> | ||
|
||
<p>Return <em>the <strong>minimum</strong> number of boxes you need to select to redistribute these </em><code>n</code><em> packs of apples into boxes</em>.</p> | ||
|
||
<p><strong>Note</strong> that, apples from the same pack can be distributed into different boxes.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> apple = [1,3,2], capacity = [4,3,1,5,2] | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> 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. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> apple = [5,5,5], capacity = [2,4,2,7] | ||
<strong>Output:</strong> 4 | ||
<strong>Explanation:</strong> We will need to use all the boxes. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n == apple.length <= 50</code></li> | ||
<li><code>1 <= m == capacity.length <= 50</code></li> | ||
<li><code>1 <= apple[i], capacity[i] <= 50</code></li> | ||
<li>The input is generated such that it's possible to redistribute packs of apples into boxes.</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |