Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: current_picking = tree[0] prev_picking = None max_picked = 0 baskets = {current_picking: [0, 0]} for i in range(len(tree)): if tree[i] != current_picking: if len(baskets) < 2: baskets[current_picking] = [0, i-1] prev_picking = current_picking current_picking = tree[i] baskets[current_picking] = [i, i] else: if tree[i] != prev_picking: baskets = { current_picking: [baskets[prev_picking][1] + 1, baskets[current_picking][1]], tree[i]: [i, i] } prev_picking = current_picking current_picking = tree[i] else: prev_picking, current_picking = current_picking, prev_picking baskets[current_picking][1] = i else: baskets[current_picking][1] = i if baskets.get(current_picking) and baskets.get(prev_picking): max_picked = max(max_picked, max([pos[1] for pos in baskets.values()]) - min([pos[0] for pos in baskets.values()]) + 1) else: max_picked = max(max_picked, baskets[current_picking][1] - baskets[current_picking][0] + 1) return max_picked
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import Counter class window: def __init__(self): self.type = Counter() def add(self, x): self.type[x] += 1 def remove(self, x): self.type[x] -= 1 if self.type[x] == 0: del self.type[x] def valid(self): return len(self.type) <= 2 class Solution: def totalFruit(self, tree): win = window() l, res = 0, 0 for r,fruit in enumerate(tree): win.add(fruit) while l < r and not win.valid(): win.remove(tree[l]) l += 1 res = max(res, r-l+1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if not tree: return 0 n = len(tree) res = 1 # starting type last_occur = {tree[0]: 0} i, j = 0, 1 # condition for while loop while j < n: new_type = tree[j] if new_type in last_occur or len(last_occur.keys()) < 2: last_occur[new_type] = j res = max(res, j-i+1) # should have new types else: count = j - i res = max(res, count) # start from the new index keys = list(last_occur.keys()) t = keys[0] if last_occur[keys[0]] < last_occur[keys[1]] else keys[1] i = last_occur[t] + 1 del last_occur[t] # new starting type at i last_occur[new_type] = j j+=1 return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: maxL = 0 l, r = 0, 0 # 0, 4 if len(set(tree)) < 3: return len(tree) while r <= len(tree): temp = tree[l:r] if len(set(temp)) < 3: maxL = max(maxL, len(temp)) r += 1 elif len(set(temp)) >= 3: l += 1 return maxL
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: #two fruit firstFruit = secondFruit = -1 firstQuant = secondQuant = 0 ans = 0 n = len(tree) i = 0 while i<n: if firstQuant == 0 or firstFruit == tree[i]: firstFruit = tree[i] firstQuant += 1 elif secondQuant == 0 or secondFruit == tree[i]: secondFruit = tree[i] secondQuant += 1 elif i>0 and tree[i-1] == firstFruit: secondFruit = tree[i] secondQuant = 1 j = i-1 while j>=0 and tree[j]==firstFruit: j-=1 firstQuant = i-1-j elif i>0 and tree[i-1] == secondFruit: firstFruit = tree[i] firstQuant = 1 j = i-1 while j>=0 and tree[j]==secondFruit: j-=1 secondQuant = i-1-j ans = max(ans,firstQuant+secondQuant) #print(firstFruit,firstQuant,secondFruit,secondQuant,ans) i+=1 return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans = i = 0 count = collections.Counter() # count = {} for j, x in enumerate(tree): count[x] += 1 # if x not in count: # count[x] = 1 # else: # count[x] += 1 while len(count) == 3: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, ar: List[int]) -> int: l=list(set(ar)) if len(l)<=2: return len(ar) else: m=0 for i in range(0,len(ar)): l=[ar[i]] # c=0 d=1 for j in range(i+1,len(ar)): if ar[j] in l: l.append(ar[j]) elif ar[j] not in l and d==1: d+=1 l.append(ar[j]) else: break m=max(m,len(l)) return m
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree)==1: return 1 window_start = 0 window_end = 2 total_max = 2 if tree[0] == tree[1]: tmp_index = 0 else: tmp_index = 1 fruit_type = set(tree[:2]) for i in range(2, len(tree)): if (tree[i] in fruit_type) or len(fruit_type)<2: window_end += 1 if tree[i] != tree[i-1]: tmp_index = i fruit_type.add(tree[i]) else: window_start = tmp_index tmp_index = i window_end += 1 fruit_type = set(tree[i-1:i+1]) #print(window_start, window_end, fruit_type) if window_end - window_start > total_max: total_max = window_end - window_start return total_max
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: dp = [[] for i in range(len(tree))] dp[-1] = [1, 1, [tree[-1]]] res = 1 for i in reversed(list(range(len(tree)-1))): if tree[i] == tree[i+1]: dp[i] = [dp[i+1][0]+1, dp[i+1][1]+1, dp[i+1][2]] elif len(dp[i+1][2]) == 1: dp[i] = [dp[i+1][0]+1, 1, [tree[i], dp[i+1][2][0]]] elif tree[i] in dp[i+1][2]: dp[i] = [dp[i+1][0]+1, 1, dp[i+1][2]] else: dp[i] = [1+dp[i+1][1], 1, [tree[i], tree[i+1]]] res = max(res, dp[i][0]) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: N = len(tree) out = 0 start = 0 olderstart = 0 newerend = newerstart = 0 olderend = olderstart = 0 older = newer = tree[0] j = 0 if N < 3: return len(tree) while newer == older and j < N: olderend = j j+= 1 if j == N: return N newer = tree[j] newerstart = j assert(newer != older) for i in range(j, N): curr = tree[i] if (curr == older or curr == newer) : if curr == older: olderend = i if curr == newer: newerend = i else: if prev == older: olderstart = newerend+1 else: olderstart = olderend+1 olderend = newerend older = newer newerstart = i newerend = i newer = curr prev = curr out = max(i-olderstart+1, out) return out
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: i = 0 count = collections.Counter() for value in tree: count[value] += 1 if len(count) > 2: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i +=1 return len(tree) - i
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree)<=2: return len(tree) n = len(tree) res = 0 start = 0 while start<n: type1, type2, type2_pos = -1, -1, -1 i = start while i<n: # i represents the end pos if type1 == -1: type1 = tree[i] elif tree[i]!=type1 and type2 == -1: type2 = tree[i] type2_pos = i elif tree[i]!=type1 and tree[i]!=type2: break i += 1 res = max(res, i-start) if i==n: break start = type2_pos return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: total_len = len(tree) if total_len == 0: return 0 rst = 1 fruit_1 = tree[0] start = 0 # Check the first fruit while tree[start] == fruit_1: start += 1 if start >= total_len: return start # Check the second fruit fruits = [fruit_1, tree[start]] fruits_order = [start-1, start] total_fruits = start+1 #print(fruits, fruits_order, total_fruits, start) for i in range(start+1, total_len): #print(\"new_fruit is\", tree[i]) if tree[i] == fruits[0]: fruits_order[0] = i total_fruits += 1 #print(\"Update the basket 0\", fruits, fruits_order, total_fruits) elif tree[i] == fruits[1]: fruits_order[1] = i total_fruits += 1 #print(\"Update the basket 1\", fruits, fruits_order, total_fruits) else: if total_fruits > rst: rst = total_fruits if fruits_order[0] > fruits_order[1]: fruits[1] = tree[i] total_fruits = i - fruits_order[1] fruits_order[1] = i else: fruits[0] = tree[i] total_fruits = i - fruits_order[0] fruits_order[0] = i #print(\"generate the new fruits order, type\") #print(fruits, fruits_order, total_fruits) if total_fruits > rst: return total_fruits return rst
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: out = 0 for i in range(len(tree)): tot = self.getTotalAtIndex(tree, i) if tot>out: out = tot if tot == len(tree): break return out def getTotalAtIndex(self, tree, i): fruit_types = set() cnt = 0 for j in range(i, len(tree)): if len(fruit_types)<2: fruit_types.add(tree[j]) if tree[j] in fruit_types: cnt+=1 else: break return cnt pass
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import defaultdict class Solution: def totalFruit(self, tree: List[int]) -> int: st = 0 max_len = 0 basket = defaultdict(int) for end in range(len(tree)): basket[tree[end]] += 1 while len(basket) > 2: basket[tree[st]] -= 1 if basket[tree[st]] == 0: del basket[tree[st]] st += 1 total = 0 for k in basket: total += basket[k] max_len = max(max_len, total) return max_len
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: n = len(tree) if n < 3: return n max_count = 0 dp = [[set(),0,set(),0] for _ in range(n)] dp[0] = [{tree[0]},1,{tree[0]},1] for i in range(1, n): if tree[i] in dp[i-1][2]: set_2 = dp[i-1][2].copy() count_2 = dp[i-1][3] + 1 else: temp = dp[i-1][0].copy() temp.add(tree[i]) set_2 = temp count_2 = dp[i-1][1] + 1 if tree[i] in dp[i-1][0]: set_1 = dp[i-1][0].copy() count_1 = dp[i-1][1] + 1 else: set_1 = {tree[i]} count_1 = 1 dp[i] = [set_1, count_1, set_2, count_2] max_count = max(max_count, max(count_1, count_2)) return max_count
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: count = collections.Counter() i = ans = 0 for j in range(len(tree)): fruit = tree[j] count[fruit] += 1 while len(count.keys()) > 2: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j-i+1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree) <= 2: return len(tree) types = {tree[0]: 0, tree[1]: 1} if tree[0] != tree[1] else {tree[0]: 1} res = 2 temp = 2 for i in range(2, len(tree)): if len(types) == 2: if tree[i] not in types: res = max(res, temp) delete = sorted(types, key=types.get)[0] temp = i - types[delete] del types[delete] types[tree[i]] = i else: temp += 1 types[tree[i]] = i else: temp += 1 types[tree[i]] = i res = max(res, temp) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans = i = 0 count = collections.Counter() for j, x in enumerate(tree): count[x] += 1 while len(count) >= 3: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: left_pointer, right_pointer = 0, 0 fruits = 0 fruits_in_basket = {} while (right_pointer < len(tree)) : if (len(fruits_in_basket) <= 2) : fruits_in_basket[tree[right_pointer]] = right_pointer right_pointer += 1 #print(fruits_in_basket) if (len(fruits_in_basket) > 2) : minimum_index = len(tree) for index in list(fruits_in_basket.values()) : minimum_index = min(minimum_index, index) left_pointer = minimum_index + 1 #print(left_pointer) fruits_in_basket.pop(tree[minimum_index]) fruits = max(fruits, (right_pointer - left_pointer)) #print(fruits) return fruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if tree == [] or tree is None: return 0 if len(tree) == 1: return 1 # [1,3,3,1] s = 0 max = 1 count = max for i in range(len(tree)): j = i + 1 b1 = tree[i] # backet 1 b2 = tree[j] # backet 2 count = 1 if b1 == b2: # continue finding a different value while incrementing while j < len(tree) and tree[j] == b1: j += 1 count += 1 if j != len(tree): b2 = tree[j] elif count > max: return count while j < len(tree): # a new number occured if tree[j] != b1 and tree[j] != b2: # compare count and max if count >= max: max = count # reset starting position break # pick up fruit and move count += 1 j += 1 if j == len(tree): if count >= max: max = count return max return max
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: char_to_count = defaultdict(int) unique_chars = 0 start = 0 end = 0 max_fruit = 0 while end < len(tree): char_to_count[tree[end]] += 1 if char_to_count[tree[end]] == 1: unique_chars += 1 while unique_chars > 2: char_to_count[tree[start]] -= 1 if char_to_count[tree[start]] == 0: unique_chars -= 1 start += 1 end += 1 max_fruit = max(max_fruit, end-start) return max_fruit
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: d = {} first = -1 second = -1 i = 0 n = len(tree) m = -1 while i<n: if tree[i] in d: d[tree[i]].append(i) i+=1 else: if len(list(d.keys()))==2: s = 0 for k,v in list(d.items()): s+=len(v) m = max(m, s) if d[first][-1]>d[second][-1]: i = d[second][-1]+1 first = first d = {} d[tree[i]] = [i] i+=1 else: i = d[first][-1]+1 first = second d = {} d[tree[i]] = [i] i+=1 elif len(list(d.keys()))==1: second = tree[i] d[tree[i]] = [i] i+=1 else: first = tree[i] d[tree[i]] = [i] i+=1 s = 0 for k,v in list(d.items()): s+=len(v) return max(m, s)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: a = Counter(tree) if len(a) <= 2: return len(tree) ans = 0 i = 0 while i < len(tree): count = 1 basket = [tree[i]] j = i + 1 while j < len(tree): if tree[j] in basket: count += 1 else: if len(basket) >= 2: break else: basket.append(tree[j]) count += 1 j += 1 if count > ans: ans = count i += 1 return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(set(tree))<=2: return len(tree) types = set() maxFruits = 0 for i in range(len(tree)): start = i res = 0 types = set() for i in range(start,len(tree)): fruitType = tree[i] if (len(types)>=2) and (fruitType not in types): break elif fruitType in types: res+=1 else: types.add(fruitType) res+=1 maxFruits = max(maxFruits,res) return maxFruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: dic = {} first = 0 i = 0 n = len(tree) ans = 0 while i<n: val = tree[i] if val in dic: dic[val] += 1 elif len(dic)<2: dic[val] = 1 else: dic[val] = 1 while len(dic) >= 2: v2 = tree[first] first+=1 dic[v2] -= 1 if dic[v2] == 0: dic.pop(v2,None) break ans = max(i-first+1, ans) i+=1 return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: fruits = tree window_start = 0 max_length = 0 fruit_frequency = {} # try to extend the range [window_start, window_end] for window_end in range(len(fruits)): right_fruit = fruits[window_end] if right_fruit not in fruit_frequency: fruit_frequency[right_fruit] = 0 fruit_frequency[right_fruit] += 1 # shrink the sliding window, until we are left with '2' fruits in the fruit frequency dictionary while len(fruit_frequency) > 2: left_fruit = fruits[window_start] fruit_frequency[left_fruit] -= 1 if fruit_frequency[left_fruit] == 0: del fruit_frequency[left_fruit] window_start += 1 # shrink the window max_length = max(max_length, window_end-window_start + 1) return max_length
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: best_attempt = 0 for i in range(len(tree)): baskets = {} for j in range(i, len(tree)): if tree[j] in baskets: baskets[tree[j]] += 1 elif len(baskets) >= 2: break else: baskets[tree[j]] = 1 attempt_val = sum(baskets.values()) if attempt_val > best_attempt: best_attempt = attempt_val if best_attempt > len(tree) - i: break return best_attempt
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: res = 0 N = len(tree) oneidx = twoidx = 0 onetype = tree[0] twotype = None left, right = 0, 0 while right < N and tree[right] == onetype: oneidx += 1 right += 1 oneidx -= 1 if right == N: return N twotype = tree[right] twoidx = right while right < N: if tree[right] == onetype: oneidx = right elif tree[right] == twotype: twoidx = right else: res = max(res, right - left) if oneidx < twoidx: left = oneidx + 1 onetype = tree[right] oneidx = right else: left = twoidx + 1 twotype = tree[right] twoidx = right right += 1 return max(res, right - left)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import OrderedDict class Solution: def totalFruit(self, tree: List[int]) -> int: last_index_of = OrderedDict() ans = l = 0 for i, t in enumerate(tree): last_index_of[t] = i last_index_of.move_to_end(t) if len(last_index_of) > 2: ans = max(ans, i-l) l = last_index_of.popitem(last=False)[1] + 1 ans = max(ans, len(tree) - l) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
''' {3:1,1:1} ''' class Solution: def totalFruit(self, tree: List[int]) -> int: first,sec=0,0 curr_hash={} if len(tree)<3:return len(tree) Max=[float('-inf')] while(first<len(tree)): first=self.first_pass(first,curr_hash,tree,Max) if first>len(tree):continue sec=self.sec_pass(sec,first,curr_hash,tree) return Max[0] def first_pass(self,pointer,hash,trees,Max): count=0 for key in hash: count+=hash[key] while(pointer<len(trees)): typ=trees[pointer] if typ in hash:hash[typ]+=1 else: if len(hash)<2: hash[typ]=1 else: break count+=1 pointer+=1 Max[0]=max(Max[0],count) return pointer def sec_pass(self,sec,first,hash,tree): while(sec<first and len(hash)>1): typ=tree[sec] if hash[typ]==1: del hash[typ] else: hash[typ]-=1 sec+=1 return sec
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(set(tree))<=2: return len(tree) types = set() maxFruits = 0 for i in range(len(tree)): start = i res = 0 types = set() if (len(tree)-start)<=maxFruits: continue for i in range(start,len(tree)): fruitType = tree[i] if (len(types)>=2) and (fruitType not in types): break elif fruitType in types: res+=1 else: types.add(fruitType) res+=1 maxFruits = max(maxFruits,res) return maxFruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: k = 2 left = 0 cnt = collections.Counter() res = 0 for j, i in enumerate(tree): if cnt[i] == 0: k-=1 cnt[i] += 1 while k<0: cnt[tree[left]] -= 1 if cnt[tree[left]] == 0: k+=1 left += 1 res = max(res, j-left +1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: c=0 p=0 for i in range(len(tree)): l=[] k=0 for j in range(i,len(tree)): if(tree[j] not in l): l.append(tree[j]) k+=1 else: k+=1 #print(l) if(len(l)>2): k=k-1 break if(k>p): p=k if(p==len(tree)): break; return p
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: max_count = 0 tree_len = len(tree) if len(tree) == 1: return 1 if len(set(tree)) in (1,2): return len(tree) for i in range(tree_len - 1): tree_set = set() tree_set.add(tree[i]) count = 1 for j in range(i+1, tree_len): if tree[j] not in tree_set and len(tree_set) == 2: break tree_set.add(tree[j]) count += 1 max_count = max(max_count, count) return max_count
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: blocks = [(k, len(list(v))) for k, v in itertools.groupby(tree)] ans = i = 0 while i < len(blocks): # We'll start our scan at block[i]. # types : the different values of tree[i] seen # weight : the total number of trees represented # by blocks under consideration types, weight = set(), 0 # For each block from i and going forward, for j in range(i, len(blocks)): # Add each block to consideration types.add(blocks[j][0]) weight += blocks[j][1] # If we have 3 types, this is not a legal subarray if len(types) >= 3: i = j-1 break ans = max(ans, weight) # If we go to the last block, then stop else: break return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: st = c = res = 0 dic = dict() for end in range(len(tree)): if tree[end] in dic: dic[tree[end]] += 1 else: dic[tree[end]] = 1 c += 1 while c > 2: dic[tree[st]] -= 1 if dic[tree[st]] == 0: del dic[tree[st]] c -= 1 st+=1 res = max(res,end-st+1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: insp = {} pos = {} mxx = -1 i = 0 while i < len(tree): f = tree[i] if f not in insp and len(insp) == 2: mnn = math.inf tod = None for of, op in list(pos.items()): mnn = min(mnn, op) if mnn == op: tod = of k = pos[tod] while k >= 0: of = tree[k] if of not in pos: break insp[of] -= 1 k -= 1 del pos[tod] del insp[tod] insp[f] = 0 elif f not in insp: insp[f] = 0 pos[f] = i insp[f] += 1 score = self.get_basketsize(insp) mxx = max(mxx,score) i += 1 return mxx def get_basketsize(self, insp): mxx = 0 nmxx = 0 for f,ct in list(insp.items()): if ct >= mxx: nmxx = max(mxx, nmxx) mxx = max(ct, mxx) elif ct >= nmxx: nmxx = max(ct, nmxx) return mxx + nmxx
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: counter = collections.Counter() l = r = 0 res = 0 while r < len(tree): counter[tree[r]] += 1 while len(counter.keys()) > 2: counter[tree[l]] -= 1 if counter[tree[l]] == 0: del counter[tree[l]] l += 1 res = max(res, sum(counter.values())) r += 1 return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: start = end = disFruits = curTotal = maxTotal = 0 fruits = {} while end < len(tree): curFruit = tree[end] frontFruit = tree[start] if curFruit not in fruits and disFruits == 2: if fruits[frontFruit] == 1: del fruits[frontFruit] disFruits -= 1 else: fruits[frontFruit] -= 1 curTotal -= 1 start += 1 else: curFruit = tree[end] if curFruit not in fruits: fruits[curFruit] = 1 disFruits += 1 else: fruits[curFruit] += 1 curTotal += 1 maxTotal = max(curTotal, maxTotal) end += 1 return maxTotal
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree) == 1: return 1 start = last1 = last2 = 0 res = 1 for i in range(1, len(tree)): if tree[i] == tree[last1]: last1 = i elif tree[i] == tree[last2]: last2 = i elif tree[last1] == tree[last2]: last1 = max(last1, last2) last2 = i else: start = min(last1, last2)+1 if last1 < last2: last1 = i else: last2 = i res = max(res, i-start+1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans = i = 0 count = collections.Counter() for j, x in enumerate(tree): count[x] += 1 while len(count) >= 3: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: def pick(start, end): res = 0 fruitType = set() for i in range(start, end): fruit = tree[i] if fruit not in fruitType and len(fruitType) >= 2: return res fruitType.add(fruit) res += 1 return res ##Main n = len(tree) cnt = Counter(tree) if len(cnt) <= 2: return n maxFruit = 0 for i in range(n): maxFruit = max(maxFruit, pick(i, n)) return maxFruit
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import defaultdict class Solution: def totalFruit(self, tree: List[int]) -> int: counter = {} longest = 0 fruits = 0 i = 0 for j in range(len(tree)): if tree[j] not in counter: fruits += 1 counter[tree[j]] = 0 counter[tree[j]] += 1 while fruits > 2: counter[tree[i]] -= 1 if counter[tree[i]] == 0: fruits -= 1 del counter[tree[i]] i += 1 longest = max(longest, j-i+1) return longest
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if(len(tree)==1): return 1 if(not tree): return 0 i = 0 j = 1 l=len(tree) ans=0 found = False second = tree[1] count = 1 while(i<l and j<l): if(tree[i]!=tree[j] and not found): second=tree[j] found=True if(found and tree[j]!=second and tree[j]!=tree[i]): ans=max(ans,count) count=0 i+=1 j=i found=False j+=1 count+=1 ans=max(ans, count) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: fruit_dict = collections.defaultdict(int) j,result,count=0,0,0 for i,t in enumerate(tree): fruit_dict[t]+=1 count+=1 while len(fruit_dict)>2 and j<len(tree): fruit_dict[tree[j]]-=1 count-=1 if fruit_dict[tree[j]]==0: del fruit_dict[tree[j]] j+=1 if len(fruit_dict)<=2: result = max(result,count) return result ''' blocks = [(k,len(list(v))) for k,v in itertools.groupby(tree)] i,result=0,0 while i<len(blocks): j=i count=0 fruit_set = set() while j<len(blocks): fruit_set.add(blocks[j][0]) count+=blocks[j][1] if len(fruit_set)>2: i=j-1 break result=max(result,count) j+=1 else: break return result '''
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ''' fruit_dict = collections.defaultdict(int) j,result,count=0,0,0 for i,t in enumerate(tree): fruit_dict[t]+=1 count+=1 while len(fruit_dict)>2 and j<len(tree): fruit_dict[tree[j]]-=1 count-=1 if fruit_dict[tree[j]]==0: del fruit_dict[tree[j]] j+=1 if len(fruit_dict)==2: result = max(result,count) return result ''' blocks = [(k,len(list(v))) for k,v in itertools.groupby(tree)] i,result=0,0 while i<len(blocks): j=i count=0 fruit_set = set() while j<len(blocks): fruit_set.add(blocks[j][0]) count+=blocks[j][1] if len(fruit_set)>2: i=j-1 break result=max(result,count) j+=1 else: break return result
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: count = collections.Counter() j = res = 0 for i in range(len(tree)): count[tree[i]] += 1 while len(count) > 2: count[tree[j]] -= 1 if count[tree[j]] == 0: del count[tree[j]] j += 1 res = max(res, sum(count.values())) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: res=[] ind=0 if len(tree)==1: return 1 if len(set(tree))<=2: return len(tree) while ind<len(tree)-1: count=0 types=set() for i in range(ind,len(tree)): types.add(tree[i]) if len(types)>2: break count+=1 res.append(count) ind+=1 return max(res)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: # winstart=0 # maxlen=0 # for winend in range(len(tree)+1): # while(len(set(tree[winstart:winend])))>=3: # winstart+=1 # maxlen=max(maxlen,winend - winstart) # return maxlen ans = i = 0 count = collections.Counter() for j, x in enumerate(tree): count[x] += 1 while len(count) >= 3: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: grp = [] x, y = tree[0], 1 for e in tree[1:]: if e==x: y+=1 else: grp.append((x,y)) x, y = e, 1 grp.append((x,y)) ret = 0 pair, pair_sum = [], 0 for i in range(len(grp)): x, y = grp[i] if x in pair: pair_sum += y else: if len(pair)<2: pair = pair + [x] pair_sum += y else: pair = [grp[i-1][0]] + [x] pair_sum = grp[i-1][1] + y # print(pair) # print(pair_sum) # print('***') ret = max(ret, pair_sum) return ret
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import defaultdict class Solution: def totalFruit(self, tree: List[int]) -> int: # freq = defaultdict(int) # freq[2] += 1 # ftypes = set() # ftypes.add(2) if not tree: return 0 freq, ftypes, ans, i = defaultdict(int), set(), float('-inf'), 0 for j, num in enumerate(tree): freq[num] += 1 ftypes.add(num) while len(ftypes) > 2: freq[tree[i]] -= 1 if freq[tree[i]] == 0: ftypes.remove(tree[i]) i += 1 ans = max(ans, j - i + 1) j += 1 return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
import collections class Solution: def totalFruit(self, tree: List[int]) -> int: res=i=0 count=Counter() for j, k in enumerate(tree): count[tree[j]] +=1 while len(count)>=3: count[tree[i]] -= 1 if count[tree[i]]==0: del count[tree[i]] i +=1 res=max(res, j-i+1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: i, j = 0, 0 max_fruit = 0 baskets = defaultdict(int) while j <= len(tree) and i < len(tree): #print(i, j, baskets) if j < len(tree) and len(set(list(baskets.keys()) + [tree[j]])) <= 2: baskets[tree[j]] += 1 j += 1 else: baskets[tree[i]] -= 1 if baskets[tree[i]] == 0: baskets.pop(tree[i]) i += 1 max_fruit = max(sum(baskets.values()), max_fruit) return max_fruit
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: i = 0 maxCol = 0 baskets = [0,0] while i < len(tree) and maxCol + i < len(tree): baskets[0] = tree[i] j = i +1 basket1Empty = True while j< len(tree): if tree[j] != baskets[0] and basket1Empty: baskets[1] = tree[j] basket1Empty = False elif tree[j] != baskets[0] and tree[j] != baskets[1]: col = j-i if col > maxCol: maxCol = col break j +=1 col = j-i if col > maxCol: maxCol = col i += 1 return maxCol
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree) == 0: return 0 types = {} numFruits = 0 maxNumFruits = 0 left = 0 right = 0 while(right < len(tree)): if tree[right] in types: types[tree[right]] += 1 else: types[tree[right]] = 1 numFruits += 1 right += 1 while len(types) > 2: if types[tree[left]] == 1: del(types[tree[left]]) else: types[tree[left]] -= 1 left += 1 numFruits -= 1 maxNumFruits = max(maxNumFruits, numFruits) return maxNumFruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree)<=2: return len(tree) n = len(tree) res,l = 0,0 while l<n: type1, type2 = -1, -1 next_l = -1 # this denote where we should place l in next subarray for r in range(l,n): if type1==-1: type1 = tree[r] elif tree[r]!=type1 and type2==-1: next_l = r type2 = tree[r] elif tree[r] != type1 and tree[r] != type2: break r += 1 res = max(res, r-l) if r==n: break l = next_l return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: hMap = {} if not tree: return 0 maxCount = 0 count = 0 for i in range(len(tree)): hMap[tree[i]] = i if len(hMap) <= 2: count += 1 maxCount = max(count, maxCount) else: j = sys.maxsize r = None for k in hMap: if hMap[k] < j: r = k j = hMap[k] hMap.pop(r, None) count = i - j # print(tree[i], hMap, count, maxCount) return maxCount
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans = i = 0 count = collections.Counter() for j, x in enumerate(tree): count[x] += 1 while len(count) >= 3: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: counter = {} start = 0 maxL = 0 for end in range(len(tree)): if tree[end] in counter: counter[tree[end]] += 1 else: counter[tree[end]] = 1 while (len(counter) > 2): counter[tree[start]] -= 1 if counter[tree[start]] == 0: del counter[tree[start]] start += 1 maxL = max(maxL, end - start + 1) return maxL
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, f: List[int]) -> int: start, count, maxFruits = 0,0,0 fruitHash = defaultdict(int) for end in range(len(f)): if fruitHash[f[end]] == 0: count += 1 fruitHash[f[end]] += 1 if count <= 2: maxFruits = max(maxFruits, end-start+1) while count > 2: fruitHash[f[start]] -= 1 start += 1 if fruitHash[f[start-1]] == 0: count -= 1 maxFruits = max(maxFruits, end-start+1) return maxFruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: max_len = 0 start = end = 0 N = len(tree) count = 0 uniq = {} while end < N: cur = tree[end] if cur not in uniq: uniq[cur] = 0 uniq[cur] += 1 if uniq[cur] == 1: count += 1 while count > 2: rem = tree[start] uniq[rem] -= 1 if uniq[rem] == 0: count -= 1 start += 1 max_len = max(max_len, end - start + 1) end += 1 return max_len
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: k = 2 start = 0 freqHashmap = {} maxFruit = 0 for end in range(len(tree)): freqHashmap[tree[end]] = freqHashmap.get(tree[end], 0) + 1 while len(freqHashmap) > k: freqHashmap[tree[start]] -= 1 if freqHashmap[tree[start]] == 0: del freqHashmap[tree[start]] start += 1 maxFruit = max(maxFruit, end - start + 1) return maxFruit
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: # 包含两个元素的(因为题目说是两个篮子)的最长子序列,采苹果不能后退也不能跳过,所以就是连续子串,采的苹果个数就指的是子串长度 # 跟之前的题目一模一样 k = 2 n = len(tree) if n < k: return n i = 0 lookup = {} ans = 0 # 1)有时候会晕是找到字典中的最小下标,还是找到字典中的重复下标 # 2)因为这里并不是因为出现重复的key而需要移动左指针,而是因为字典里元素太多了而要移动左指针 # 3)要找到重复了的key,那么就需要在插入之前去判断, 遇到重复的键值i = max(i, 1+lookUp[nums[j]]) # 4)字典长度超标,不怕重复,因为重复的录入同一个键值一直记录的该key最后一次出现的位置, # 4.1)i_min = min(lookup.values()), 另 i= i_min+1, 之所以要一个中间变量i_min,是因为后面要用它去删除键值 # 4.2)同时删除这个key,否则你每次取min都会找到这个数懂吗 for j in range(n): lookup[tree[j]] = j if len(lookup) <= k: ans = max(ans, j-i+1) else: i_min = min(lookup.values()) del lookup[tree[i_min]] i = i_min + 1 return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree): res = cur = count_b = a = b = 0 for c in tree: cur = cur + 1 if c in (a, b) else count_b + 1 count_b = count_b + 1 if c == b else 1 if b != c: a, b = b, c res = max(res, cur) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: # 包含两个元素的(因为题目说是两个篮子)的最长子序列,采苹果不能后退也不能跳过,所以就是连续子串,采的苹果个数就指的是子串长度 # 跟之前的题目一模一样 k = 2 n = len(tree) if n < k: return n i = 0 lookup = {} ans = 0 # 1)有时候会晕是找到字典中的最小下标,还是找到字典中的重复下标 # 2)因为这里并不是因为出现重复的key而需要移动左指针,而是因为字典里元素太多了而要移动左指针 # 3)要找到重复了的key,那么就需要在插入之前去判断, 遇到重复的键值i = max(i, 1+lookUp[nums[j]]) # 4)字典长度超标,不怕重复,因为重复的录入同一个键值一直记录的该key最后一次出现的位置, # 4.1)i_min = min(lookup.values()), 另 i= i_min+1, 之所以要一个中间变量i_min,是因为后面要用它去删除键值 # 4.2)同时删除这个key,否则你每次取min都会找到这个数懂吗 for j in range(n): lookup[tree[j]] = j if len(lookup) > k: i_min = min(lookup.values()) del lookup[tree[i_min]] i = i_min + 1 ans = max(ans, j-i+1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree) -> int: if len(tree) < 3: return len(tree) seq_len = [[tree[0], 0]] for f in tree: if seq_len[-1][0] == f: seq_len[-1][1] += 1 else: seq_len.append([f, 1]) most = seq_len[0][1] if len(seq_len) > 1: curr_two_types = set([seq_len[0][0], seq_len[1][0]]) most = curr_most = seq_len[0][1] + seq_len[1][1] for i in range(2, len(seq_len)): if seq_len[i][0] in curr_two_types: curr_most += seq_len[i][1] else: curr_most = seq_len[i][1] + seq_len[i-1][1] curr_two_types = set([seq_len[i][0], seq_len[i-1][0]]) most = max(most, curr_most) return most
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ''' Sliding window: the window can at most only have two distinct numbers Find the longest sliding window Technique: keep the last seen index of each fruit type in my current sliding window seen: { x: i, y: j } Whenever a new type of fruit is ahead, fast forward the left boundary to min(i,j) + 1 ''' ans = 0 lo, hi = -1, 0 seen = {} for hi in range(len(tree)): if tree[hi] not in seen and len(seen) == 2: # tree[hi] is not in seen and seen already has 2 elements remove = (float('inf'), None) for typ in seen: remove = min(remove, (seen[typ], typ)) lo = seen.pop(remove[1]) seen[tree[hi]] = hi #print(lo, hi, seen) ans = max(ans, hi-lo) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: count, i = {}, 0 for j, v in enumerate(tree): count[v] = count.get(v, 0) + 1 if len(count) > 2: count[tree[i]] -= 1 if count[tree[i]] == 0: del count[tree[i]] i += 1 return j - i + 1
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: max_count = pair_count = last_count = 0 fruit_1 = fruit_2 = last_fruit = None for fruit_type in tree: if fruit_type == fruit_1: pair_count += 1 if fruit_type == last_fruit: last_count += 1 else: last_fruit = fruit_type last_count = 1 elif fruit_type == fruit_2: pair_count += 1 if fruit_type == last_fruit: last_count += 1 else: last_fruit = fruit_type last_count = 1 elif fruit_1 == None: fruit_1 = last_fruit = fruit_type last_count = 1 pair_count += 1 elif fruit_2 == None: fruit_2 = last_fruit = fruit_type last_count = 1 pair_count += 1 else: fruit_1 = last_fruit fruit_2 = last_fruit = fruit_type max_count = max(max_count, pair_count) pair_count = last_count+1 last_count = 1 return max(max_count, pair_count)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans=0 past=[] last=[] for i in range(len(tree)): #print(past,last) if len(past)==0: past.append(tree[i]) last.append(i) ans=max((i-last[0]+1),ans) continue if len(past)==1: if tree[i] not in past: past.append(tree[i]) last.append(i) ans=max((i-last[0]+1),ans) continue if tree[i] not in past: bt=i-1 ele=tree[bt] while tree[bt]==ele: bt-=1 bt+=1 past=[tree[bt],tree[i]] last=[bt,i] ans=max((i-last[0]+1),ans) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: n = len(tree) if n < 3: return n max_count = 0 set_1 = {tree[0]} count_1 = 1 set_2 = set_1.copy() count_2 = 1 for i in range(1, n): if tree[i] in set_2: count_2 += 1 else: temp = set_1.copy() temp.add(tree[i]) set_2 = temp count_2 = count_1 + 1 if tree[i] in set_1: count_1 += 1 else: set_1 = {tree[i]} count_1 = 1 max_count = max(max_count, max(count_1, count_2)) return max_count
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: l = 0 count = {} for r, value in enumerate(tree): count[value] = count.get(value,0)+1 if len(count) > 2: count[tree[l]] -= 1 if count[tree[l]] == 0: del count[tree[l]] l += 1 return r-l+1
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: res, start, distince_count, visited = 0, 0, 0, [0] * len(tree) for i, c in enumerate(tree): if visited[c] == 0: distince_count += 1 visited[c] += 1 while distince_count > 2: visited[tree[start]] -= 1 if visited[tree[start]] == 0: distince_count -= 1 start += 1 res = max(res, i - start + 1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: res = cur = count_b = a = b = 0 for c in tree: cur = cur + 1 if c in (a, b) else count_b + 1 count_b = count_b + 1 if c == b else 1 if b != c: a, b = b, c res = max(res, cur) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ''' 行不通的 example4就不满足 #最长的bi-value subarray #记录到i处一共几种数出现,两个减出来=2意味着中间只有两种。subarray标准套路. from collections import Counter n = len(tree) record = [] for i in range(n): a = len(Counter(tree[:i+1])) record.append(a) print(record) dic = {} ans = 0 dic[0] = [-1,-1] #记录第一次出现和最后一次出现的位置 for i in range(n): if record[i] not in dic.keys(): dic[record[i]] = [i,i] else: dic[record[i]][1] = i if record[i]-2 in dic.keys(): print(i,dic[record[i]],dic[record[i]-2]) ans = max(ans,dic[record[i]][1]-dic[record[i]-2][1]) print('ans=',ans) return ans ''' #sliding window, two pointers:x,i n = len(tree) dic = {} x = 0 for i in range(n): if tree[i] not in dic.keys(): dic[tree[i]] = 1 else: dic[tree[i]] += 1 if len(dic) > 2: dic[tree[x]] -= 1 if dic[tree[x]]==0 : del dic[tree[x]] x +=1 return i-x+1
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: deq = [] currentTwo = set() result = 0 temp = 0 for e in tree: if len(deq) == 0: currentTwo.add(e) deq.append((e, 1)) temp = 1 elif e in currentTwo: if e == deq[-1][0]: deq[-1] = (e, deq[-1][1]+1) temp += 1 else: deq.append((e,1)) temp += 1 else: previousOne = deq[-1][0] currentTwo = set([previousOne, e]) deq = deq[-1:] temp = deq[-1][1] deq.append((e, 1)) temp += 1 result = max(result, temp) return result
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree) -> int: if len(tree) < 3: return len(tree) # i = 0 # while i < len(tree) and tree[i] == tree[0]: # i += 1 # if i == len(tree): # return i # most_fruit = i + 1 # first_type, f_counter = tree[0], i # second_type, s_counter = tree[i], 1 seq_len = [[tree[0], 0]] for f in tree: if seq_len[-1][0] == f: seq_len[-1][1] += 1 else: seq_len.append([f, 1]) most = curr_most = seq_len[0][1] # print(seq_len) if len(seq_len) > 1: curr_two_types = set([seq_len[0][0], seq_len[1][0]]) most = curr_most = seq_len[0][1] + seq_len[1][1] for i in range(2, len(seq_len)): if seq_len[i][0] in curr_two_types: curr_most += seq_len[i][1] else: curr_most = seq_len[i][1] + seq_len[i-1][1] curr_two_types = set([seq_len[i][0], seq_len[i-1][0]]) most = max(most, curr_most) # print(\"---\", most) return most
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: letter_count = collections.defaultdict(int) left, right = 0, 0 while right < len(tree): letter_count[tree[right]] += 1 right += 1 if len(letter_count) > 2: letter_count[tree[left]] -= 1 if letter_count[tree[left]] == 0: del letter_count[tree[left]] left += 1 return right - left
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree): ans = cur = one = two = count_one = count_two = 0 for i in tree: if i not in (one, two): cur = count_two + 1 else: cur += 1 if i != two: one, two = two, i count_two = 1 else: count_two += 1 ans = max(ans, cur) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if not len(tree): return 0 count_max = 0 start = 0 f_num = tree[0] tracking = set() tracking.add(f_num) last_change = 0 current_num = tree[0] for i in range(1, len(tree)): if tree[i] not in tracking: if len(tracking) >= 2: count_max = max(i-start, count_max) tracking.remove(f_num) tracking.add(tree[i]) f_num = current_num start = last_change else: tracking.add(tree[i]) if tree[i] != current_num: last_change = i f_num = current_num current_num = tree[i] count_max = max(len(tree)-start, count_max) return count_max
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: # def totalFruit(self, tree): # # scan the array # fruit_block = [] # #fruit_block entity = [left_most index in fruit, weight] # for i in range(len(tree)): # if i == 0 or tree[i] != tree[i-1]: # fruit_block.append([i, 1 ]) # elif tree[i] == tree[i-1]: # fruit_block[-1][1] += 1 # # print(fruit_block) # max_fruit = 0 # i = 0 # while i < len(fruit_block): # old_i = i # type_fruit = [] # weight = 0 # for j in range(i, len(fruit_block)): # if tree[fruit_block[j][0]] not in type_fruit: # type_fruit.append(tree[fruit_block[j][0]]) # weight += fruit_block[j][1] # # print(\"type fruit \", type_fruit) # # print(\"weight \", weight) # if len(type_fruit) > 2: # # print(\"break\") # i = j -1 # break # max_fruit = max(max_fruit, weight) # if j == len(fruit_block): # break # if i == old_i: # break # return max_fruit # def totalFruit(self, tree: List[int]) -> int: # greattest_count = 0 # for i in range(len(tree)): # if i > 0 and tree[i] == tree[i-1]: # print(i) # continue # if i > 2 and tree[i] == tree[i-2] and tree[i-1] == tree[i-3] and tree[i] != tree[i-1] and self.checkRepeat(tree, i): # #add function to define tree[i-1] == the newest tree after ones that look like tree[i] # print(i) # continue # fruit = self.pickFruit(i, tree) # if fruit > greattest_count: # greattest_count = fruit # return greattest_count # # print(count) # # print(greattest_count) # return greattest_count # def checkRepeat(self,tree, i): # for j in range(i, len(tree)): # if tree[j] != tree[i]: # return tree[j]==tree[i-1] # return True # def pickFruit(self, i, tree): # count = 0 # basket=[\"\", \"\"] # for j in range(i, len(tree)): # if basket[0] == \"\": # basket[0] = tree[j] # # print(\"basket1: \", basket[0]) # count += 1 # # print(\"basket1\") # elif tree[j] == basket[0]: # count+=1 # # print(\"already in basket1\") # elif basket[1] == \"\": # basket[1] = tree[j] # count += 1 # # print(\"basket2: \", basket[1]) # elif tree[j] == basket[1]: # count += 1 # # print(\"already in basket2\") # elif tree[j] not in basket: # return count # if j == len(tree)-1: # return count def totalFruit(self, tree: List[int]) -> int: basket1 = -1 basket2 = -1 max_fruit = 0 start = 0 end = 0 i = 0 while i < len(tree): if tree[i] == basket1 or tree[i] == basket2: if tree[i] != tree[i-1]: end = i i += 1 continue if basket1 == -1: basket1 = tree[i] end = i i += 1 continue if basket2 == -1: basket2 = tree[i] end = i i += 1 continue print(i) print(end) weight = i - start max_fruit = max(max_fruit, weight) #max_fruit = 2 - 0 = 2 i = end # 1 start = end # 1 basket1 = -1 basket2 = -1 # if start == 0: # weight = len(tree) # else: # weight = len(tree) - start - 1 weight = len(tree) - start max_fruit = max(max_fruit, weight) return max_fruit
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, t: List[int]) -> int: st,a,b,a_hi,res,b_hi = 0,t[0],-1,0,0,0 for i,v in enumerate(t): if v != a and b == -1: b = v b_hi = i elif v == a: a_hi = i elif v == b: b_hi = i elif v not in (a,b): res = max(res,i-st) st_m = min(a_hi,b_hi) a,b,st,a_hi,b_hi = t[st_m+1],v,st_m+1,max(a_hi,b_hi),i return max(res,len(t)-st)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: dic = {} i = 0 j = 0 MAX = 1 if tree == None or len(tree) == 0: return None while j < len(tree): if len(dic) <= 2: dic[tree[j]] = j j+=1 if len(dic) > 2: mins = len(tree) -1 for val in list(dic.values()): mins = min(mins,val) i = mins + 1 del dic[tree[mins]] MAX = max(MAX,j-i) return MAX
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if not tree: return 0 l = 0 mp = collections.defaultdict(int) fruits = 1 for i in range(0, len(tree)): mp[tree[i]] += 1 while len(mp) > 2: mp[tree[l]] -= 1 if mp[tree[l]] == 0: del mp[tree[l]] l += 1 fruits = max(fruits, i-l+1) return fruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: left = 0 right = 0 window = set() res = 0 last_index = collections.defaultdict(int) for right in range(len(tree)): fruit_type = tree[right] last_index[fruit_type] = right if tree[right] in window or len(window) < 2: window.add(tree[right]) else: window.add(tree[right]) while len(window) > 2: fruit_type = tree[left] if left == last_index[fruit_type]: window.remove(fruit_type) left += 1 res = max(res, right - left + 1) return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
# longest run that only contains two symbols? # 6:52 -> 7:01 TLE on naive n^2 solution class Solution: def totalFruit(self, tree: List[int]) -> int: counts = 0 starts = {} start = end = 0 longest = 0 def find_earliest(starts): earliest = len(tree) for key in starts: earliest = min(starts[key], earliest) return earliest while end < len(tree): # print(start, end, starts) current = tree[end] if current in starts: starts[current] = end end += 1 else: if counts == 2: earliest = find_earliest(starts) del starts[tree[earliest]] start = earliest + 1 counts -= 1 else: starts[current] = end counts += 1 end += 1 longest = max(end - start, longest) return max(end - start, longest) # longest = 0 # def contains_two_symbols(subarray): # c = Counter(subarray) # contains = 0 # for key in c: # if c[key]: # contains += 1 # return contains < 3 # for i in range(len(tree)): # for j in range(i + 1, len(tree) + 1): # if contains_two_symbols(tree[i:j]): # longest = max(longest, j - i) # return longest
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ans = i = 0 count = collections.defaultdict(int) for j, ftype in enumerate(tree): count[ftype] += 1 while len(count) > 2: count[tree[i]] -= 1 if count[tree[i]] == 0: count.pop(tree[i]) i += 1 ans = max(ans, j - i + 1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: # length of the longest subarray containing at most 2 distinct elements if len(tree) == 0: return 0 i, j, n_kind, res = 0, 0, 1, 1 cnts = [0,] * len(tree) cnts[tree[0]] += 1 while True: while n_kind <= 2: res = max(res, j-i+1) j += 1 if j >= len(tree): return res # ? if cnts[tree[j]] == 0: n_kind += 1 cnts[tree[j]] += 1 while n_kind > 2: cnts[tree[i]] -= 1 if cnts[tree[i]] == 0: n_kind -= 1 i += 1 return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import deque class Solution: def totalFruit(self, tree: List[int]) -> int: maxFruits = 0 fruitTypes = set() queue = deque() for i in tree: if i in fruitTypes or len(fruitTypes) < 2: fruitTypes.add(i) else: prevFruit = queue[-1] j = len(queue)-2 while j >= 0 and queue[j] == prevFruit: j -= 1 while j >=0: queue.popleft() j -= 1 for k in list(fruitTypes): if k == prevFruit: continue else: fruitTypes.remove(k) fruitTypes.add(i) queue.append(i) maxFruits = max(len(queue), maxFruits) return maxFruits
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: l, res, check = 0, 0, dict() for i, t in enumerate(tree): check[t] = check.get(t, 0) + 1 if len(check) <= 2: res = max(res, i - l + 1) while len(check) > 2: check[tree[l]] -= 1 if check[tree[l]] == 0: del check[tree[l]] l += 1 return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree) <= 2: return len(tree) collection_list = [tree[0]] counter = 1 _max = 1 rep = 1 for i in range(1, len(tree)): if tree[i] in collection_list: if tree[i] == tree[i-1]: rep += 1 else: rep = 1 counter += 1 if counter > _max: _max = counter else: if len(collection_list) == 1: collection_list.append(tree[i]) counter += 1 rep = 1 if counter > _max: _max = counter else: collection_list = [tree[i-1], tree[i]] counter = 1 + rep rep = 1 return _max
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import Counter class Solution: def totalFruit(self, tree: List[int]) -> int: d = collections.defaultdict(int) left = 0 total = 0 for idx, i in enumerate(tree): d[i] += 1 while(len(d) > 2): d[tree[left]] -= 1 if(d[tree[left]] == 0): del d[tree[left]] left += 1 total = max(total, idx - left + 1) return total
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: if len(tree) == 0 or len(tree) == 1: return len(tree); A = [-1]*2 B = [-1]*2 f1 = -1 f2 = -1 n = len(tree) maxi = 1 i=0 while i<n: if tree[i]==f1 or tree[i]==f2: if tree[i]==f1: A[1]=i else: B[1]=i else: if tree[i-1]==f1: f2=tree[i] A[0]=B[1]+1 B[0]=B[1]=i else: f1=tree[i] B[0]=A[1]+1 A[0]=A[1]=i maxi = max(maxi,max(A[1],B[1])-min(A[0],B[0])) i = i+1 return maxi+1
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: idx, baskets, res = -1, collections.deque(maxlen=2), 0 for i, x in enumerate(tree): if x not in [y[0] for y in baskets]: if len(baskets) == 2: res, idx = max(res, i - idx - 1), baskets[0][1] baskets.append([x, i]) else: if x == baskets[0][0]: baskets.popleft() baskets.append([x, i]) else: baskets[1][1] = i return max(res, len(tree) - 1 - idx)
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: store = collections.defaultdict(lambda:0) ans = 0 i=0 for k,val in enumerate(tree): store[val]+=1 while len(store)>2: store[tree[i]]-=1 if store[tree[i]]==0: del store[tree[i]] i+=1 ans = max(ans,k-i+1) return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: f1, f2= [-1,-1],[-1,-1] res, fruit1, fruit2 = 0, -1, -1 for i in range(len(tree)): if tree[i] not in [fruit1,fruit2]: if f2[1]>f1[1]: f2[0] = f1[1]+1 f1 = [i,i] fruit1 = tree[i] else: f1[0] = f2[1]+1 f2 = [i,i] fruit2 = tree[i] elif tree[i] == fruit1: f1[1] = i else: f2[1] = i res = max(res,max(f1[1],f2[1]) - min(f1[0],f2[0])) return res+1
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: # compress the tree compressedTree = [] current = [tree[0], 0] for index in range(len(tree)): fruit = tree[index] if fruit != current[0]: compressedTree.append(current) current = [fruit, 1] else: current[1] += 1 compressedTree.append(current) ans = i = 0 while i < len(compressedTree): # We'll start our scan at block[i]. # types : the different values of tree[i] seen # weight : the total number of trees represented # by blocks under consideration types, weight = set(), 0 # For each block from i and going forward, for j in range(i, len(compressedTree)): # Add each block to consideration types.add(compressedTree[j][0]) weight += compressedTree[j][1] # If we have 3 types, this is not a legal subarray if len(types) >= 3: i = j-1 break ans = max(ans, weight) # If we go to the last block, then stop else: break return ans
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
from collections import defaultdict class Solution: def totalFruit(self, tree: List[int]) -> int: l, r = 0, 0 d = defaultdict(int) res = 0 for r in range(len(tree)): d[tree[r]] += 1 while len(d) > 2: #print(l) d[tree[l]] -= 1 if d[tree[l]] == 0: d.pop(tree[l]) l += 1 res = max(res, r - l + 1) #print(r,l) return res # while r < len(tree): # while len(d) <= 2: # r += 1 # if r < len(tree): # d[tree[r]] += 1 # else: # break # if len(d) > 2 or r == len(tree): # res = max(res, r - l) # # print(\"1s\", r, l) # while len(d) > 2: # d[tree[l]] -= 1 # if d[tree[l]] == 0: # d.pop(tree[l]) # l += 1 # # print(\"2s\", r, l) # return res
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.  If you cannot, stop. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure?   Example 1: Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1]. Example 2: Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1]. Example 3: Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2]. Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.   Note: 1 <= tree.length <= 40000 0 <= tree[i] < tree.length
class Solution: def totalFruit(self, tree: List[int]) -> int: ''' [0,1,2,2] ^ [3,3,3,1,2,1,1,2,3,3,4] ''' n = len(tree) res = 0 window = defaultdict(int) count = 0 left = 0 for idx in range(n): curr = tree[idx] if window[curr] == 0: count += 1 window[curr] += 1 if count <= 2: res = max(res, idx - left + 1) while count > 2: window[tree[left]] -= 1 if window[tree[left]] == 0: count -= 1 left += 1 return res