You are visiting a farm that has a single row of fruit trees arranged from 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. However, the owner has some strict rules that you must follow:


	You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
	Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
	Once you reach a tree with fruit that cannot fit in your baskets, you must stop.


Given the integer array fruits, return the maximum number of fruits you can pick.

&nbsp;
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, we would only 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 pick from trees [1,2].


&nbsp;
Constraints:


	1 &lt;= fruits.length &lt;= 105
	0 &lt;= fruits[i] &lt; fruits.length


class Solution:
    def totalFruit(self, fruits: List[int]) -> int:
        ans=0
        fruitdict=defaultdict()
        stack=[]
        i,j=0,0

        while j<len(fruits):
            if fruits[j] not in fruitdict and len(fruitdict)<2:
                stack.append(fruits[j])
                fruitdict[fruits[j]]=j
                j+=1

            elif  fruits[j] in fruitdict:
                fruitdict[fruits[j]]=j
                j+=1

            else:  
                if fruitdict[stack[0]]>fruitdict[stack[1]]  :
                    i = fruitdict[stack[1]]+1
                    del fruitdict[stack[1]]
                    stack.pop()
                else:
                    i = fruitdict[stack[0]]+1
                    del fruitdict[stack[0]] 
                    stack.pop(0)              
            
            ans=max(ans,j-i)
        return ans
class Solution {
    public int totalFruit(int[] fruits) {
        if (fruits == null || fruits.length == 0) {
            return 0;
        }
        int start = 0, end = 0, res = 0;
        HashMap<Integer, Integer> map = new HashMap<>(); //key = type of fruit on tree, value = last index / newest index of that fruit

        while (end < fruits.length) {
            if (map.size() <= 2) {
                map.put(fruits[end], end);
                end++;
            }

            if (map.size() > 2) {
                int leftMost = fruits.length;
                for (int num : map.values()) {
                    leftMost = Math.min(leftMost, num);
                }
                map.remove(fruits[leftMost]);
                start = leftMost + 1;
            }

            res = Math.max(res, end - start);
        }
        return res;
    }
}
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int i=0, j=0, ans = 1;
        unordered_map<int, int>mp;
        while(j<fruits.size()){
            mp[fruits[j]]++;
            
            if(mp.size()<2){
                ans = max(ans, j-i+1);
                j++;
            }
            else if(mp.size()==2){
                ans = max(ans, j-i+1);
                j++;
            }
            else if(mp.size()>2){
                while(mp.size()>2){
                    mp[fruits[i]]--;
                    if(mp[fruits[i]]==0)
                        mp.erase(fruits[i]);
                    i++;
                }
                if(mp.size()==2){
                    ans = max(ans, j-i+1);
                }
                j++;
            }
        }
        return ans;
    }
};
var totalFruit = function(fruits) {
    let myFruits = {};
    let n = fruits.length;
    let windowStart = 0;
    let ans = -Number.MAX_VALUE;
    
    for(let windowEnd = 0; windowEnd < n; windowEnd++) {
        let fruit = fruits[windowEnd];
        
        if(fruit in myFruits) {
            myFruits[fruit]++;
        }
        else {
            myFruits[fruit] = 1;
        }
        
        while(Object.keys(myFruits).length > 2) {
            let throwOut = fruits[windowStart];
            myFruits[throwOut]--;
            if(myFruits[throwOut] == 0) {
                delete myFruits[throwOut];
            }
            windowStart++;
        }
        ans = Math.max(ans, windowEnd - windowStart + 1);
    }
    return ans;
};
Fruit Into Baskets