Member-only story
Maximum Fruits ππππ₯ Into Baskets π§Ί| Coding Interview | Sliding Window Pattern
I heard from many sources that this problem is a notorious one, that many big companies ask this question, its different aspects or slightly modified versions. This article will discuss how this problem is a specific version of the more general problem that we have already discussed, Longest Substring with K Distinct Characters.
ππ€© so many companies have asked this one question!
This problem is part of the 30 days preparation plan.
Description
We are at a farm with a row of trees π³π²π΄ arranged from left to right. The trees are represented by different characters in an array of fruits and where fruits[i] is the type of fruit ith tree holds. We can only pick one fruit from a tree.
The goal is to collect as many fruits as possible but only two baskets. We can start from any tree but once started; we cannot skip a tree.
So we have to find the maximum number of fruits we can collect from the farm using just two baskets.
Example 1:Input: fruits = [a, b, b, a, c]
Output: 4Explanation: We will get fruits from the first 4 trees using 2 baskets.Example 2: