A browsing session visits n items in order, each tagged with an integer category id. Let U be the number of distinct categories that appear anywhere in the session. Find the length of the shortest contiguous run of visits that contains at least one item from every one of those U categories.
Line 1: an integer n.
Line 2: n space-separated integers, the category ids in visit order.
A single integer: the length of the shortest contiguous window that contains all U distinct categories.
Example 1
Input
7 1 2 3 1 2 3 4
Expected
4
Explanation
There are 4 distinct categories {1,2,3,4}. The shortest run covering all four is [1,2,3,4] at the end, of length 4.
Example 2
Input
5 7 7 7 7 7
Expected
1
Explanation
Only one category exists, so a single visit already covers all categories: length 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →