Fruit Into Baskets
Given list of different fruits, each number represent different fruit type and not its count, we have only two basket return maximum of fruits we can collect i.e longest continuous subarray containing at most 2 different numbers
Problem Statement
You are visiting a farm with a single row of fruit trees, arranged 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, following these rules:
- You only have two baskets, and each basket can hold only one type of fruit. There's no limit on how much fruit each basket can hold.
- Starting from any tree you choose, you must pick exactly one fruit from every tree (including the starting tree) while moving to the right. Each picked fruit must fit into one of your baskets.
- Once you reach a tree whose fruit can't fit in either basket, you must stop.
Given fruits, return the maximum number of fruits you can collect.
Examples
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 instead, we would only be able to 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 be able to pick from trees [1, 2].
Constraints
1 ≤ fruits.length ≤ 10⁵(this value appeared as "105" in the original text — it most likely means10⁵, but you may want to double-check the original source)0 ≤ fruits[i] < fruits.length
Solution
This problem can be reframed as: find the longest continuous subarray that contains at most 2 different fruit types. This is a classic sliding window problem.
Steps
- We use a
currentWindowmap to track how many of each fruit type are inside our current window. - We move the
endpointer forward, one tree at a time, and add each fruit tocurrentWindow. - If our window ever contains more than 2 different fruit types, we need to shrink it from the left:
- We remove the fruit at the
startposition from our count. If its count drops to0, we remove it from the map entirely. - We move
startforward, and keep shrinking until we're back down to at most 2 fruit types.
- We remove the fruit at the
- After each step, we check if the current window's size (
end - start + 1) is the largest we've seen so far, and update our answer if so. - Once we've gone through the whole array, our answer holds the maximum number of fruits we can collect.
Code
var totalFruit = function (fruits) {
let start = 0;
let longestSubString = 0;
let currentWindow = new Map();
for (let end = 0; end < fruits.length; end++) {
let curr = fruits[end];
currentWindow.set(curr, (currentWindow.get(curr) ?? 0) + 1);
while (currentWindow.size > 2) {
let startElement = fruits[start];
let newCount = currentWindow.get(startElement) - 1;
if (newCount > 0) {
currentWindow.set(startElement, newCount);
} else {
currentWindow.delete(startElement);
}
start++;
}
longestSubString = Math.max(longestSubString, end - start + 1);
}
return longestSubString;
};