Fruit Into Baskets - Complete Solution Guide
Fruit Into Baskets is LeetCode problem 904, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow: You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice, you
Detailed Explanation
The problem "Fruit Into Baskets" asks us to find the maximum number of fruits we can pick from a row of trees, given that we have only two baskets and each basket can hold only one type of fruit. We must start picking from any tree and continue moving right, picking one fruit from each tree, until we encounter a fruit type that doesn't fit in either of our two baskets. The goal is to maximize the number of fruits picked under these constraints.
Solution Approach
The solution uses a sliding window technique combined with a hash map to keep track of the count of each fruit type in the current window. The window expands to the right as long as there are at most two distinct fruit types in it. If the window contains more than two types, we shrink it from the left until the condition is met. In each step, we keep track of the maximum window size observed so far, which represents the maximum number of fruits that can be picked.
Step-by-Step Algorithm
- Step 1: Initialize a hash map `basket` to store the frequency of each fruit type within the sliding window, the left pointer of the window `left` to 0, and the maximum picked fruits `max_picked` to 0.
- Step 2: Iterate through the `fruits` array using the right pointer `right`. For each fruit encountered, update its count in the `basket` hash map.
- Step 3: While the number of distinct fruit types in the `basket` (i.e., the size of the hash map) is greater than 2, shrink the window from the left.
- Step 4: To shrink the window, decrement the count of the leftmost fruit type in the `basket`. If its count becomes 0, remove it from the `basket`.
- Step 5: Move the left pointer `left` one step to the right.
- Step 6: After each iteration of the outer loop, update `max_picked` to be the maximum of its current value and the current window size (right - left + 1).
- Step 7: Return `max_picked` after the iteration finishes.
Key Insights
- Insight 1: The problem can be efficiently solved using a sliding window approach to find the longest continuous subarray containing at most two distinct elements (fruit types).
- Insight 2: A hash table (or dictionary) is useful to track the frequency of each fruit type within the current window.
- Insight 3: Maintaining the window size and updating it based on the number of distinct fruit types ensures we meet the problem's constraints.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Hash Table, Sliding Window.
Companies
Asked at: Deutsche Bank, Walmart Labs.