Member-only story

Maximum Fruits πŸ‘πŸ’πŸ“πŸ₯­ Into Baskets 🧺| Coding Interview | Sliding Window Pattern

Ganesh Prasad
5 min readMar 1, 2022

--

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.

Maximum fruits into baskets

πŸ˜‚πŸ€© 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: 4
Explanation: We will get fruits from the first 4 trees using 2 baskets.Example 2:

--

--

Ganesh Prasad
Ganesh Prasad

Written by Ganesh Prasad

Backend Developer at Appscrip | C++ veteran, πŸ’œ Dart

Responses (1)