|
| 1 | +# Problem: Fruits into Baskets |
| 2 | + |
| 3 | +LeetCode problem: [904. Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/). |
| 4 | + |
| 5 | +Given an array of characters where each character represents a fruit tree, you are given two baskets and your goal is to put maximum number of fruits in each basket. The only restriction is that each basket can have only one type of fruit. |
| 6 | + |
| 7 | +You can start with any tree, but once you have started you can’t skip a tree. You will pick one fruit from each tree until you cannot, i.e., you will stop when you have to pick from a third fruit type. |
| 8 | + |
| 9 | +Write a function to return the maximum number of fruits in both the baskets. |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +Example 1: |
| 14 | + |
| 15 | +```plaintext |
| 16 | +Input: Fruit = ['A', 'B', 'C', 'A', 'C'] |
| 17 | +Output: 3 |
| 18 | +Explanation: We can put 2 'C' in one basket and one 'A' in the other from the subarray ['C', 'A', 'C'] |
| 19 | +``` |
| 20 | + |
| 21 | +Example 2: |
| 22 | + |
| 23 | +```plaintext |
| 24 | +Input: Fruit = ['A', 'B', 'C', 'B', 'B', 'C'] |
| 25 | +Output: 5 |
| 26 | +Explanation: We can put 3 'B' in one basket and two 'C' in the other basket. |
| 27 | +This can be done if we start with the second letter: ['B', 'C', 'B', 'B', 'C'] |
| 28 | +``` |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +This problem is quite similar to the [Longest Substring with K Distinct Characters](./03-longest-substring-with-k-distinct-characters.md) problem where `K` (representing the number of fruit types) is equal to `2`. |
| 33 | + |
| 34 | +Complexity analysis: |
| 35 | + |
| 36 | +- Time complexity: O(N) |
| 37 | +- Space complexity: O(1) |
| 38 | + |
| 39 | +```python |
| 40 | +def totalFruit(fruits: List[int]) -> int: |
| 41 | + maximum_fruits = 0 |
| 42 | + |
| 43 | + window_start = 0 |
| 44 | + window_counter = Counter() |
| 45 | + for window_end in range(len(fruits)): |
| 46 | + window_counter[fruits[window_end]] += 1 |
| 47 | + |
| 48 | + while len(window_counter) > 2: |
| 49 | + window_counter[fruits[window_start]] -= 1 |
| 50 | + |
| 51 | + if window_counter[fruits[window_start]] == 0: |
| 52 | + window_counter.pop(fruits[window_start]) |
| 53 | + |
| 54 | + window_start += 1 |
| 55 | + |
| 56 | + maximum_fruits = max(maximum_fruits, (window_end - window_start) + 1) |
| 57 | + |
| 58 | + return maximum_fruits |
| 59 | +``` |
| 60 | + |
| 61 | +## Similar Problem |
| 62 | + |
| 63 | +LeetCode problem: [159. Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/). |
| 64 | + |
| 65 | +Given a string, find the length of the longest substring in it with at most two distinct characters. |
| 66 | + |
| 67 | +### Solution |
| 68 | + |
| 69 | +This problem is exactly similar to the parent [Fruits into Baskets](#problem-fruits-into-baskets) problem. |
| 70 | + |
| 71 | +Complexity analysis: |
| 72 | + |
| 73 | +- Time complexity: O(N) |
| 74 | +- Space complexity: O(1) |
| 75 | + |
| 76 | +```python |
| 77 | +def lengthOfLongestSubstringTwoDistinct(s: str) -> int: |
| 78 | + longest_length = 0 |
| 79 | + |
| 80 | + window_start = 0 |
| 81 | + window_counter = Counter() |
| 82 | + for window_end in range(len(s)): |
| 83 | + window_counter[s[window_end]] += 1 |
| 84 | + |
| 85 | + while len(window_counter) > 2: |
| 86 | + window_counter[s[window_start]] -= 1 |
| 87 | + |
| 88 | + if window_counter[s[window_start]] == 0: |
| 89 | + window_counter.pop(s[window_start]) |
| 90 | + |
| 91 | + window_start += 1 |
| 92 | + |
| 93 | + longest_length = max(longest_length, (window_end - window_start) + 1) |
| 94 | + |
| 95 | + return longest_length |
| 96 | +``` |
0 commit comments