Class Solution
-
- All Implemented Interfaces:
public final class Solution
3479 - Fruits Into Baskets III.
Medium
You are given two arrays of integers,
fruits
andbaskets
, each of lengthn
, wherefruits[i]
represents the quantity of the <code>i<sup>th</sup></code> type of fruit, andbaskets[j]
represents the capacity of the <code>j<sup>th</sup></code> basket.From left to right, place the fruits according to these rules:
Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
Each basket can hold only one type of fruit.
If a fruit type cannot be placed in any basket, it remains unplaced.
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = 4,2,5, baskets = 3,5,4
Output: 1
Explanation:
fruits[0] = 4
is placed inbaskets[1] = 5
.fruits[1] = 2
is placed inbaskets[0] = 3
.fruits[2] = 5
cannot be placed inbaskets[2] = 4
.
Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = 3,6,1, baskets = 6,4,7
Output: 0
Explanation:
fruits[0] = 3
is placed inbaskets[0] = 6
.fruits[1] = 6
cannot be placed inbaskets[1] = 4
(insufficient capacity) but can be placed in the next available basket,baskets[2] = 7
.fruits[2] = 1
is placed inbaskets[1] = 4
.
Since all fruits are successfully placed, we return 0.
Constraints:
n == fruits.length == baskets.length
<code>1 <= n <= 10<sup>5</sup></code>
<code>1 <= fruitsi, basketsi<= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Integer
numOfUnplacedFruits(IntArray fruits, IntArray baskets)
-
-
Method Detail
-
numOfUnplacedFruits
final Integer numOfUnplacedFruits(IntArray fruits, IntArray baskets)
-
-
-
-