Fruits Into Baskets III Problem
Description
LeetCode Problem 3479.
You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the i^th type of fruit, and baskets[j] represents the capacity of the j^th 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 in baskets[1] = 5.
- fruits[1] = 2 is placed in baskets[0] = 3.
- fruits[2] = 5 cannot be placed in baskets[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 in baskets[0] = 6.
- fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.
- fruits[2] = 1 is placed in baskets[1] = 4.
Since all fruits are successfully placed, we return 0.
Constraints:
n == fruits.length == baskets.length1 <= n <= 10^51 <= fruits[i], baskets[i] <= 10^9
Solution
Approach: Segment Tree with Descent
Fruits are placed in order, and each one goes into the leftmost unused basket whose capacity is at least the fruit's quantity. A plain scan would cost O(n) per fruit, so we need a faster way to find that basket.
Build a segment tree over baskets where every node stores the maximum capacity in its range. To place a fruit of size x:
- If the root's maximum is below
x, no basket fits and the fruit stays unplaced. - Otherwise walk down from the root: go left if the left child's maximum is at least
x, else go right. This lands on the leftmost fitting basket. - Set that basket's capacity to 0 and update the maximums on the way back up, so it is never used again.
Complexity: O(n log n) time and O(n) extra space, where n is the number of baskets.
class SegmentTree:
__slots__ = ["nums", "tr"]
def __init__(self, nums):
self.nums = nums
n = len(nums)
self.tr = [0] * (n << 2)
self.build(1, 1, n)
def build(self, u, l, r):
if l == r:
self.tr[u] = self.nums[l - 1]
return
mid = (l + r) >> 1
self.build(u << 1, l, mid)
self.build(u << 1 | 1, mid + 1, r)
self.pushup(u)
def modify(self, u, l, r, i, v):
if l == r:
self.tr[u] = v
return
mid = (l + r) >> 1
if i <= mid:
self.modify(u << 1, l, mid, i, v)
else:
self.modify(u << 1 | 1, mid + 1, r, i, v)
self.pushup(u)
def query(self, u, l, r, v):
if self.tr[u] < v:
return -1
if l == r:
return l
mid = (l + r) >> 1
if self.tr[u << 1] >= v:
return self.query(u << 1, l, mid, v)
return self.query(u << 1 | 1, mid + 1, r, v)
def pushup(self, u):
self.tr[u] = max(self.tr[u << 1], self.tr[u << 1 | 1])
class Solution:
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
tree = SegmentTree(baskets)
n = len(baskets)
ans = 0
for x in fruits:
i = tree.query(1, 1, n, x)
if i < 0:
ans += 1
else:
tree.modify(1, 1, n, i, 0)
return ans