Watch 10 video solutions for Fruit Into Baskets, a medium level problem involving Array, Hash Table, Sliding Window. This walkthrough by take U forward has 256,144 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.lengthProblem Overview: You receive an integer array where each value represents a fruit type on a tree. Starting from any index, you can collect fruits moving right, but your baskets can hold only two distinct fruit types. The task is to find the maximum number of fruits you can pick while maintaining this constraint.
Approach 1: Sliding Window with HashMap (O(n) time, O(1) space)
This problem reduces to finding the longest subarray with at most two distinct values. The most reliable method uses a sliding window. Maintain two pointers left and right representing the current window, and a hash map storing fruit counts inside the window. As you iterate with the right pointer, update the map with the current fruit type. If the map grows beyond two types, move the left pointer forward while decrementing counts until only two types remain.
The key insight: every valid window represents a sequence where both baskets hold a fruit type. Expanding the window maximizes collected fruits, while shrinking keeps the constraint valid. Each element enters and leaves the window at most once, giving O(n) time complexity. Because the map stores at most two fruit types, the space complexity is effectively O(1). This technique is a classic application of the sliding window pattern on an array with a frequency hash table.
Approach 2: Two Pointer Technique (O(n) time, O(1) space)
The two pointer variation tracks the last contiguous segment of the second fruit type instead of storing full counts. Use variables to remember the last fruit type, the second last fruit type, and the length of the most recent streak. When you encounter a fruit matching one of the two tracked types, extend the window. If a third type appears, reset the window so it starts from the previous streak of the last fruit type.
This approach eliminates the explicit hash map and relies on pointer movement and state tracking. The algorithm still processes each element once, giving O(n) time and constant O(1) space. It is slightly harder to reason about but performs well when memory overhead must be minimized.
Recommended for interviews: The sliding window with a hash map is the expected solution. It clearly expresses the constraint "at most two distinct values" and generalizes easily to problems like "longest substring with k distinct characters." Showing the brute reasoning first and then implementing the optimal sliding window demonstrates strong problem‑solving skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window with HashMap | O(n) | O(1) | General solution for longest subarray with at most two distinct elements. Most readable and common in interviews. |
| Two Pointer Technique (State Tracking) | O(n) | O(1) | Useful when avoiding hash maps and optimizing constant memory usage. |