Two kinds
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A row of trees grows fruit, and fruit[i] is the kind of fruit on tree i. You have two baskets, and each basket can only hold one kind of fruit.
Starting at any tree, you walk to the right, picking one fruit from every tree, until you reach a tree whose kind fits in neither basket. Return the most fruit you can pick.
In other words: return the length of the longest stretch of fruit with at most two different values.
Examples
Example 1
- Input
- fruit = [4, 9, 4]
- Output
- 3
All three trees have only two kinds between them.
Example 2
- Input
- fruit = [5, 6, 7, 7]
- Output
- 3
Start at tree 1: kinds 6, 7, 7.
Example 3
- Input
- fruit = [8, 6, 8, 9, 9, 9]
- Output
- 4
Start at tree 2: 8, 9, 9, 9.
Rules
fruithas between 0 and 100,000 whole numbers.
Words on this page, in plain English (1)Open if any word is new to you
- Length
- How many items a list (or letters a string) has. The last position is always
length - 1.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
Under the fruit story, the question is simple: what's the longest stretch of the list, items in a row, with at most two different values?
2. Solve an example by hand
[8, 6, 8, 9, 9, 9]. Start at tree 0: 8, 6, 8, then 9 is a third kind, stop: 3 fruit. Start at tree 2: 8, 9, 9, 9, two kinds: 4 fruit. That's the best.
When the third kind arrived, we didn't need to start from scratch: we just had to drop trees from the left until only two kinds were left.
3. The slow way
Try every start and walk right until a third kind appears: O(n²) for long lists of only two kinds.
4. Spot the pattern
The longest stretch that follows a rule ("at most 2 kinds"): a growing and shrinking window. To know how many different kinds are in the window, keep a Map of counts and look at its size.
There's one detail: when a kind's count drops to 0, delete it from the Map. Otherwise size still counts it, even though it's gone from the window.
5. The window idea
At most two kinds in the window
fruit
- best =
- 0
counts (Map)
| kind | count |
|---|---|
| 3 | 1 |
function twoKinds(fruit) { const counts = new Map(); // kind → how many in the window let left = 0; let best = 0; for (let right = 0; right < fruit.length; right++) { counts.set(fruit[right], (counts.get(fruit[right]) ?? 0) + 1); while (counts.size > 2) { const kind = fruit[left]; counts.set(kind, counts.get(kind) - 1); if (counts.get(kind) === 0) { counts.delete(kind); // no longer in the window } left++; } best = Math.max(best, right - left + 1); } return best;}Tree 0 has kind 3. The window now has 1 kind.
6. The code, line by line
const counts = new Map(); // kind → how many in the window let left = 0; let best = 0;countsdescribes the window: how many of each kind are in it.for (let right = 0; right < fruit.length; right++) { counts.set(fruit[right], (counts.get(fruit[right]) ?? 0) + 1);Add the tree at
rightto the window.while (counts.size > 2) { const kind = fruit[left]; counts.set(kind, counts.get(kind) - 1); if (counts.get(kind) === 0) { counts.delete(kind); // no longer in the window } left++; }Three kinds is too many. Drop trees from the left: lower their kind's count, and delete the kind entirely when its count hits 0, so
counts.sizeis right.best = Math.max(best, right - left + 1);Now the window holds at most two kinds: keep its length if it's the best.
7. How fast is it?
| Every start | Sliding window | |
|---|---|---|
| Time | O(n²) | O(n) |
| Extra memory | O(1) | O(1): the Map holds at most 3 kinds |
The same code solves "at most k kinds" for any k: just change the 2.
Common mistakes
- Not deleting kinds at 0.
counts.sizethen never goes back down, and the window shrinks to nothing. - Resetting at a third kind. Starting a new window just at the new tree loses the trees before it that still fit, like the
2, 2before a3in2, 2, 3.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Two kinds
Read the 18 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function twoKinds(fruit) { const counts = new Map(); // kind → how many in the window let left = 0; let best = 0; for (let right = 0; right < fruit.length; right++) { counts.set(fruit[right], (counts.get(fruit[right]) ?? 0) + 1); while (counts.size > 2) { const kind = fruit[left]; counts.set(kind, counts.get(kind) - 1); if (counts.get(kind) === 0) { counts.delete(kind); // no longer in the window } left++; } best = Math.max(best, right - left + 1); } return best;}