You are given two arrays of integers, fruits
and baskets
, each of length n
, where fruits[i]
represents the quantity of the ith
type of fruit, and baskets[j]
represents the capacity of the jth
basket.
From left to right, place the fruits according to these rules:
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.length
1 <= n <= 100
1 <= fruits[i], baskets[i] <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the Fruits Into Baskets problem is like trying every single possible picking combination. We start by considering the smallest possible range of fruit and expand it until we find the longest valid picking sequence. We keep track of the best one found so far.
Here's how the algorithm would work step-by-step:
def fruits_into_baskets_brute_force(fruits):
maximum_fruits_picked = 0
for starting_index in range(len(fruits)):
fruits_in_baskets = {}
fruits_picked_from_start = 0
for current_index in range(starting_index, len(fruits)):
fruit_type = fruits[current_index]
# Check if adding this fruit exceeds basket limit
if fruit_type not in fruits_in_baskets and len(fruits_in_baskets) == 2:
break
if fruit_type not in fruits_in_baskets:
fruits_in_baskets[fruit_type] = 0
fruits_in_baskets[fruit_type] += 1
fruits_picked_from_start += 1
# Update max fruits picked
maximum_fruits_picked = max(maximum_fruits_picked, fruits_picked_from_start)
return maximum_fruits_picked
The best way to solve this is to use a 'sliding window' to keep track of the different types of fruit in our baskets. We efficiently expand the window to include more fruit, and shrink it when we have too many types, always aiming to maximize the fruit we pick.
Here's how the algorithm would work step-by-step:
def fruits_into_baskets_two(fruits):
window_start = 0
fruit_frequency = {}
min_removed = float('inf')
for window_end in range(len(fruits)):
right_fruit = fruits[window_end]
if right_fruit not in fruit_frequency:
fruit_frequency[right_fruit] = 0
fruit_frequency[right_fruit] += 1
# Shrink the sliding window until we have at most two fruit types
while len(fruit_frequency) > 2:
left_fruit = fruits[window_start]
fruit_frequency[left_fruit] -= 1
if fruit_frequency[left_fruit] == 0:
del fruit_frequency[left_fruit]
window_start += 1
# Calculate the number of fruits removed to keep 2 types
current_length = window_end - window_start + 1
# Update the minimum number of removed fruits
min_removed = min(min_removed, len(fruits) - current_length)
# If min_removed is still infinity, it means all fruits can be put in one basket
if min_removed == float('inf'):
return 0
return min_removed
Case | How to Handle |
---|---|
Empty or null input array | Return 0 as no fruits can be picked. |
Array with only one type of fruit | Return the length of the array since all fruits can be picked. |
Array with two types of fruit | Return the length of the array since all fruits can be picked. |
Array with all fruits of the same type | Return the length of the array. |
Array with a long sequence of one fruit type followed by another single fruit | Sliding window needs to correctly adjust the start index after a new fruit type appears. |
Input array containing only two distinct fruit types spread unevenly. | Sliding window algorithm needs to correctly identify the longest sequence. |
Integer overflow if the fruit count is extremely large. | Use a data type that can handle large numbers, such as long or consider breaking if input fruit array is excessively large. |
Starting from a sub optimal starting position. | Test all possible starting position to determine optimal starting point that produces maximum length. |