Question: Python solution to the following algorithm question: 'Pick Fruits'. Janet went to the orchard to pick fruit. There are 2 baskets that can hold countless

Python solution to the following algorithm question: 'Pick Fruits'.

Janet went to the orchard to pick fruit. There are 2 baskets that can hold countless fruits, but each baskert can only hold one type of fruit. Start from the tree at any position and pick it to the right. Stop picking when one of the following two conditions occurs, 1. encountered the third type of fruit, no basket can be put, 2. meet the end. Returns the maximum number of fruits that can be picked.The fruit array is represented by arr.

The length of the array does not exceed 100,000.

Example

Given arr=[1,2,1,3,4,3,5,1,2], return 3.

 Select [1, 2, 1] or [3, 4, 3]. The length is 3. 

Given arr=[1,2,1,2,1,2,1], return 7.

 Select [1, 2, 1, 2, 1, 2, 1]. The length is 7.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!