Two-Crate Harvest
mediumAn orchard's trees stand in a single row; trees[i] is the fruit type on tree i. You walk the row once, picking one fruit per tree, but you carry only two crates and each crate holds one type of fruit. You may start at any tree, but once you hit a third type you must stop.
Return the maximum number of fruits you can collect — i.e. the length of the longest contiguous run containing at most 2 distinct types.
Example 1
in trees = [1, 2, 1]
out 3
Types {1, 2} — take all three.
Example 2
in trees = [0, 1, 2, 2]
out 3
[1, 2, 2] uses types {1, 2}.
Example 3
in trees = [1, 2, 3, 2, 2]
out 4
[2, 3, 2, 2] uses types {2, 3}.
Constraints
- · 1 <= len(trees) <= 2000
- · 0 <= trees[i] <= 10^4
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.