Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
The solution I came up with just uses a HashSet. public static int getFruitCount...

K Curtis

Dec 26, 2022

The solution I came up with just uses a HashSet.

public static int getFruitCount(char[] fruitTrees) { int maxFruit = 0; int windowStart = 0; HashSet fruitBasket = new HashSet(); for(int windowEnd = 0; windowEnd < fruitTrees.length; windowEnd++) { char currentFruit = fruitTrees[windowEnd]; fruitBasket.add(currentFruit); while (fruitBasket.size() > 2) { fruitBasket.remove(fruitTrees[windowStart]); windowStart++; } maxFruit = Math.max(maxFruit, windowEnd - windowStart+1); }

return maxFruit; }

0

0

Comments
Comments

On this page