Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python Sample Code: In a garden there are N fruit trees growing in a row. Each tree produces one fruit of some type. Little Amy
Python
Sample Code:
In a garden there are N fruit trees growing in a row. Each tree produces one fruit of some type. Little Amy goes to the garden to collect some fruit. She has two baskets that can hold any number of fruit, but she only wants to put one type of fruit in each basket. When Amy enters the garden, she goes to some tree, collects the fruit from it (every tree produces exactly one fruit) and then goes to the next tree to the right. Amy continues her walk until she encounters a tree with a fruit that she cannot put in any of her baskets (each of them already has a fruit ofa different type), or when the garden ends. Amy never throws any fruit away from her basket. What is the maximal number of fruit she can collect? Write a function: def solution(A) that, given an array A of N integers, representing consecutive types of fruit in the garden, returns the maximal number of fruit Amy can collect. For example, given A [1,2,1,3, 4,3, 5,1, 2] the function should return 3. Amy can collect fruit of types 1 and 2 from the beginning of the garden or all fruits of types 3 and 4. Given A = [1, 2, 1, 2, 1, 2, 1] the function should return 7, Amy can collect all the fruit from the garden. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; :each element of array A lis an integer within the range lo. 1,000,000
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started