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 of size N, 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 the 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.
Example 1:
Input:
N = 3
fruits [ ] = { 2, 1, 2 }
Output: 3
Explanation: We can pick from all three trees. 
 
Example 2:
Input:
N = 6
fruits [ ] = { 0, 1, 2, 2, 2, 2 }
Output: 5
Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5].
Your Task:
You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ fruits_{i} < N

Difficulty: MEDIUM
Tags: Data structures

Amortized analysis
Skill types: Amortized analysis

Data structures
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3



class Solution:

    def sumSubarrayMins(self, N, fruits):

        # Code here

Solutions:
class Solution:

	def sumSubarrayMins(self, N, fruits):
		first = fruits[0]
		second = -1
		i = 0
		j = 1
		ans = 1
		while j < N:
			if fruits[j] != first and fruits[j] != second:
				if second == -1:
					second = fruits[j]
				else:
					i = j
					while fruits[i - 1] == fruits[j - 1]:
						i -= 1
					if first != fruits[i]:
						first = fruits[j]
					else:
						second = fruits[j]
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def sumSubarrayMins(self, N, fruits):
		k = 2
		type = {}
		i = 0
		j = 0
		max_fruits = 0
		while j < N:
			fruit = fruits[j]
			type[fruit] = type.get(fruit, 0) + 1
			if len(type) > k:
				max_fruits = max(max_fruits, j - i)
				while len(type) > k:
					type[fruits[i]] = type[fruits[i]] - 1
					if type[fruits[i]] == 0:
						del type[fruits[i]]
					i += 1
			j += 1
		max_fruits = max(max_fruits, j - i)
		return max_fruits


import collections

class Solution:

	def sumSubarrayMins(self, N, fruits):
		(l, total, result) = (0, 0, 0)
		count = collections.defaultdict(int)
		for i in range(len(fruits)):
			count[fruits[i]] += 1
			total += 1
			while len(count) > 2:
				f = fruits[l]
				count[f] -= 1
				total -= 1
				l += 1
				if not count[f]:
					count.pop(f)
			result = max(result, total)
		return result


class Solution:

	def sumSubarrayMins(self, N, fruits):
		(pt1, pt2) = (0, 0)
		hash_map = {}
		ct = 0
		no_fruits = 0
		max_fruits = 0
		while pt1 <= pt2 and pt2 < N:
			if fruits[pt2] in hash_map:
				hash_map[fruits[pt2]] += 1
			else:
				ct += 1
				hash_map[fruits[pt2]] = 1
			no_fruits += 1
			while ct > 2:
				hash_map[fruits[pt1]] -= 1
				no_fruits -= 1
				if hash_map[fruits[pt1]] == 0:
					del hash_map[fruits[pt1]]
					ct -= 1
				pt1 += 1
			max_fruits = max(max_fruits, no_fruits)
			pt2 += 1
		return max_fruits


import sys
from collections import defaultdict

class Solution:

	def sumSubarrayMins(self, N, fruits):
		d = defaultdict(int)
		p = 0
		i = 0
		j = 0
		while j < N:
			d[fruits[j]] += 1
			while len(d) > 2:
				d[fruits[i]] -= 1
				if d[fruits[i]] == 0:
					del d[fruits[i]]
				i += 1
			p = max(j - i + 1, p)
			j += 1
		return p


class Solution:

	def sumSubarrayMins(self, N, fruits):
		dic = {}
		cnt = 0
		ans = 0
		j = 0
		for i in range(N):
			el = fruits[i]
			if el not in dic:
				dic[el] = 1
				cnt += 1
				while len(dic) > 2 and j < i:
					dic[fruits[j]] -= 1
					cnt -= 1
					if dic[fruits[j]] == 0:
						del dic[fruits[j]]
					j += 1
			else:
				dic[el] += 1
				cnt += 1
			ans = max(ans, cnt)
		return ans


class Solution:

	def sumSubarrayMins(self, N, fruits):
		(i, j, ans) = (0, 0, 0)
		state = {}
		while j < N:
			state[fruits[j]] = 1 + state.get(fruits[j], 0)
			while len(state) > 2:
				state[fruits[i]] = -1 + state.get(fruits[i])
				if state[fruits[i]] == 0:
					state.pop(fruits[i])
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def sumSubarrayMins(self, n, fruits):
		hashmap = {}
		(l, r) = (0, 0)
		res = 0
		while r < n:
			while len(hashmap) >= 2 and fruits[r] not in hashmap:
				hashmap[fruits[l]] -= 1
				if hashmap[fruits[l]] == 0:
					del hashmap[fruits[l]]
				l += 1
			hashmap[fruits[r]] = 1 + hashmap.get(fruits[r], 0)
			res = max(res, r - l + 1)
			r += 1
		return res


class Solution:

	def sumSubarrayMins(self, N, fruits):
		(fp, lp, mp) = (0, 0, {})
		ans = 0
		while lp < N:
			if fruits[lp] not in mp and len(mp) == 2:
				while len(mp) == 2 and fp < lp:
					mp[fruits[fp]] -= 1
					if mp[fruits[fp]] == 0:
						mp.pop(fruits[fp])
					fp += 1
			if fruits[lp] in mp:
				mp[fruits[lp]] += 1
			else:
				mp[fruits[lp]] = 1
			ans = max(ans, lp - fp + 1)
			lp += 1
		return ans


class Solution:

	def sumSubarrayMins(self, n, fruits):
		hm = {}
		l = 0
		r = 0
		k = 2
		ans = 0
		while r < n:
			if fruits[r] in hm:
				hm[fruits[r]] += 1
			else:
				hm[fruits[r]] = 1
			if len(hm) > k:
				hm[fruits[l]] -= 1
				if hm[fruits[l]] == 0:
					del hm[fruits[l]]
				l += 1
			ans = max(r - l + 1, ans)
			r += 1
		return ans


class Solution:

	def sumSubarrayMins(self, N, fruits):
		res = 0
		r = 0
		d = dict()
		for i in range(N):
			d[fruits[i]] = d.get(fruits[i], 0) + 1
			while len(d) > 2:
				d[fruits[r]] = d.get(fruits[r], 0) - 1
				if d[fruits[r]] == 0:
					del d[fruits[r]]
				r += 1
			if len(d) <= 2:
				ans = i - r + 1
				res = max(res, ans)
		return res


class Solution:

	def sumSubarrayMins(self, N, fruits):
		fruitsCnt = dict()
		maxFruits = 0
		startIdx = 0
		for endIdx in range(len(fruits)):
			if fruits[endIdx] in fruitsCnt:
				fruitsCnt[fruits[endIdx]] += 1
			else:
				fruitsCnt[fruits[endIdx]] = 1
			while startIdx < endIdx and len(fruitsCnt) > 2:
				fruitsCnt[fruits[startIdx]] -= 1
				if fruitsCnt[fruits[startIdx]] == 0:
					fruitsCnt.pop(fruits[startIdx], None)
				startIdx += 1
			maxFruits = max(maxFruits, endIdx - startIdx + 1)
		return maxFruits


class Solution:

	def sumSubarrayMins(self, N, fruits):
		tree = {}
		(i, maxf) = (0, 0)
		for j in range(N):
			tree[fruits[j]] = 1 + tree.get(fruits[j], 0)
			while i < N and len(tree) > 2:
				tree[fruits[i]] -= 1
				if not tree[fruits[i]]:
					del tree[fruits[i]]
				i += 1
			maxf = max(maxf, j - i + 1)
		return maxf


class Solution:

	def sumSubarrayMins(self, n, a):
		d = {}
		f = 0
		for i in range(n):
			if len(d) == 2:
				f = 1
				break
			elif a[i] not in d.keys():
				d[a[i]] = 1
			else:
				d[a[i]] += 1
		if f == 0:
			i += 1
		j = 0
		s = sum(d.values())
		ans = s
		while i < n:
			if a[i] in d.keys():
				d[a[i]] += 1
				s += 1
			else:
				while j < i and len(d) == 2:
					d[a[j]] -= 1
					s -= 1
					if d[a[j]] == 0:
						del d[a[j]]
					j += 1
				d[a[i]] = 1
				s += 1
			i += 1
			ans = max(ans, s)
		return ans


class Solution:

	def sumSubarrayMins(self, N, fruits):
		(i, j) = (0, 0)
		d = {}
		maxi = 1
		while j < N:
			d[fruits[j]] = d.get(fruits[j], 0) + 1
			if len(d) <= 2:
				maxi = max(maxi, j - i + 1)
				j += 1
			else:
				while len(d) > 2:
					d[fruits[i]] -= 1
					if d[fruits[i]] == 0:
						d.pop(fruits[i])
					i += 1
					if len(d) == 2:
						maxi = max(maxi, j - i + 1)
				j += 1
		return maxi


class Solution:

	def sumSubarrayMins(self, N, fruits):
		basket = {}
		max_picked = 0
		left = 0
		for right in range(len(fruits)):
			basket[fruits[right]] = basket.get(fruits[right], 0) + 1
			while len(basket) > 2:
				basket[fruits[left]] -= 1
				if basket[fruits[left]] == 0:
					del basket[fruits[left]]
				left += 1
			max_picked = max(max_picked, right - left + 1)
		return max_picked


class Solution:

	def sumSubarrayMins(self, N, fruits):
		(start, end, count, ans) = (0, 0, 0, 0)
		cnt = [0] * (max(fruits) + 1)
		while end < N:
			if cnt[fruits[end]] == 0:
				count += 1
			cnt[fruits[end]] += 1
			end += 1
			while start < end and count > 2:
				cnt[fruits[start]] -= 1
				if cnt[fruits[start]] == 0:
					count -= 1
				start += 1
			ans = max(ans, end - start)
		return ans