Loading...
Loading...
You are given a non-empty list of non-negative integers nums.
The degree of a list is the highest number of times any one value appears in it. For example, the degree of [4,1,4,4] is 3, because 4 appears three times.
A subarray is a contiguous part of nums.
Return the length of the shortest subarray of nums whose degree is equal to the degree of nums.
Input: nums = [1,2,2,3,1]
Output: 2
Explanation: The values 1 and 2 both appear twice, and 3 appears once, so the degree of nums is 2.
The subarray [2,2] contains 2 twice, so its degree is also 2.
A subarray of length 1 always has degree 1, so no shorter subarray works. The answer is 2.
Input: nums = [1,2,2,3,1,4,2]
Output: 6
Explanation: The value 2 appears three times, at indices 1, 2, and 6, so the degree of nums is 3.
No other value appears three times.
The subarray [2,2,3,1,4,2] runs from index 1 to index 6 and contains 2 three times, so its degree is 3.
Every shorter subarray leaves out at least one copy of 2, so its degree is at most 2. The answer is 6.
nums.length ≤5×104nums[i] ≤49999