Fruit Into Baskets
The list fruits gives the type fruits[i] of the fruit on tree i, along a row. You walk along the row picking one fruit from each tree, starting anywhere and never skipping a tree. You have two baskets and each can hold only one type of fruit.
Stop when a tree has a third type you cannot carry. Return the most fruit you can pick.
Example 1
fruits [1,2,1]
returns 3
Only two types, take all 3.
Example 2
fruits [0,1,2,2]
returns 3
Start at index 1: types 1 and 2 give 3 fruits.
Constraints
1 <= len(fruits) <= 400 <= fruits[i] <= 20
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Longest Complete Vowel StretchMedium
- Longest Ones After Deleting OneMedium
- Longest Palindromic SubstringMedium
- Longest Repeating Character ReplacementMedium
- Longest Stretch With No Letter ThriceMedium
- Longest Substring Without Repeating CharactersMedium
- Max Consecutive Ones With FlipsMedium
- Minimum Size Subarray SumMedium