Skip to content

Commit c7a1db4

Browse files
committed
Problem: Fruits into Baskets
1 parent 02e2959 commit c7a1db4

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
```

03-sliding-window/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ This technique is often used for solving problems that require processing or ana
1010
| :--------------------------------------------------------------------------------------------------: | :---------------------: |
1111
| [Maximum Sum Subarray of Size K](./01-maximum-sum-subarray-of-size-k.md) | :star2: |
1212
| [Smallest Subarray with a Given Sum](./02-smallest-subarray-with-a-given-sum.md) | :star2: |
13-
| [Longest Substring with K Distinct Characters](./03-longest-substring-with-k-distinct-characters.md) | :star2: :star2: |
13+
| [Longest Substring with K Distinct Characters](./03-longest-substring-with-k-distinct-characters.md) | :star2: :star2: |
14+
| [Fruits into Baskets](./04-fruits-into-baskets.md) | :star2: :star2: |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This repository contains my solutions and notes for the Educative's [Grokking Co
3838
| [Maximum Sum Subarray of Size K](./03-sliding-window/01-maximum-sum-subarray-of-size-k.md) |
3939
| [Smallest Subarray with a Given Sum](./03-sliding-window/02-smallest-subarray-with-a-given-sum.md) |
4040
| [Longest Substring with K Distinct Characters](./03-sliding-window/03-longest-substring-with-k-distinct-characters.md) |
41+
| [Fruits into Baskets](./03-sliding-window/04-fruits-into-baskets.md) |
4142

4243
## Courses
4344

0 commit comments

Comments
 (0)