You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
Given the integer array fruits, return the maximum number of fruits you can pick.
Example 1:
Input: fruits = [1,2,1] Output: 3 Explanation: We can pick from all 3 trees.
Example 2:
Input: fruits = [0,1,2,2] Output: 3 Explanation: We can pick from trees [1,2,2]. If we had started at the first tree, we would only pick from trees [0,1].
Example 3:
Input: fruits = [1,2,3,2,2] Output: 4 Explanation: We can pick from trees [2,3,2,2]. If we had started at the first tree, we would only pick from trees [1,2].
Constraints:
1 <= fruits.length <= 1050 <= fruits[i] < fruits.lengthWhen 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:
Imagine you are walking down a straight line of fruit trees and can only collect fruits using at most two distinct types of baskets. The brute force approach checks every possible starting tree and every possible ending tree to see how many total fruits you can gather without breaking the two-type limit.
Here's how the algorithm would work step-by-step:
def total_fruit(tree_list):
max_fruits_collected = 0
# Test every possible starting tree to explore all contiguous subarrays from this point
for start_position in range(len(tree_list)):
for end_position in range(start_position, len(tree_list)):
unique_fruit_types = set()
# Scan the current stretch of trees and collect the unique types present
for current_position in range(start_position, end_position + 1):
unique_fruit_types.add(tree_list[current_position])
# Verify if the stretch respects the basket limit of at most two fruit types
if len(unique_fruit_types) <= 2:
current_fruit_count = (end_position - start_position) + 1
if current_fruit_count > max_fruits_collected:
max_fruits_collected = current_fruit_count
# Return the maximum valid fruit count found across all tested tree stretches
return max_fruits_collectedInstead of checking every single starting point, we can use a moving window to keep track of the fruit types we collect along our path. Whenever we stumble upon a third type of fruit, we slide our collection area forward until we are back down to just two types, always keeping track of the longest stretch we managed to cover.
Here's how the algorithm would work step-by-step:
def total_fruit(tree_fruit_types):
fruit_frequency_map = {}
window_start_index = 0
maximum_fruits_collected = 0
for window_end_index, current_fruit_type in enumerate(tree_fruit_types):
fruit_frequency_map[current_fruit_type] = fruit_frequency_map.get(current_fruit_type, 0) + 1
# Shrink the window from the left if we exceed two distinct fruit types.
while len(fruit_frequency_map) > 2:
left_fruit_type = tree_fruit_types[window_start_index]
fruit_frequency_map[left_fruit_type] -= 1
if fruit_frequency_map[left_fruit_type] == 0:
del fruit_frequency_map[left_fruit_type]
window_start_index += 1
# Track the maximum length of valid trees visited with at most two fruit types.
current_stretch_length = window_end_index - window_start_index + 1
if current_stretch_length > maximum_fruits_collected:
maximum_fruits_collected = current_stretch_length
return maximum_fruits_collected| Case | How to Handle |
|---|---|
| Empty or null fruit array | Return zero immediately as no fruits can be collected from an empty orchard |
| Array with length one or two | Return the exact length of the array since all elements fit within the two available baskets |
| Orchard with all trees producing the exact same fruit type | The sliding window expands to encompass the entire array and returns the total length |
| Orchard with alternating fruit types | The window will incorporate all elements seamlessly because the distinct fruit count never exceeds two |
| Massive input array scaling up to maximum constraints | An optimized sliding window with a hash map achieves linear time complexity to prevent time limit exceeded errors |
| Fruit types represented by extreme integer values or characters | Using a hash map or frequency array allows handling arbitrary fruit identifiers without relying on contiguous indexing |
| Frequently changing fruit types requiring constant basket eviction | The left pointer of the sliding window shrinks efficiently until the distinct fruit count drops back to two |
| Long sequence of unique fruits preventing large windows | The algorithm correctly resets and tracks the maximum length found across multiple valid subsegments |