You're a candy enthusiast with a collection of various flavored candies! ๐ญ Your mom wants you to share k consecutive candies with your little sister, but being the flavor connoisseur you are, you want to maximize the variety of flavors you get to keep.
Given an array candies where candies[i] represents the flavor of the i-th candy, you need to determine which k consecutive candies to give away so that you retain the maximum number of unique flavors.
Goal: Find the optimal subarray of length k to remove that leaves you with the most diverse flavor collection.
Example: If you have candies [1,2,1,3,4,3,2] and need to share k=3 candies, you could give away [1,3,4] (indices 2-4) and keep flavors [1,2,3,2] which has 3 unique flavors!
Input & Output
Visualization
Time & Space Complexity
Single pass through array with constant time operations for each element
Hash maps store at most n unique flavors
Constraints
- 1 โค candies.length โค 105
- 1 โค candies[i] โค 105
- 1 โค k โค candies.length
- All array values are positive integers