Given a non empty integer array nums, define the degree of nums as the maximum frequency of any value in the array. Return the minimum length of a contiguous subarray of nums whose degree is equal to the degree of the entire array.
Constraints:
Input: nums = [1, 2, 2, 3, 1]-List[int]
Output: 2-int
Explanation: The degree of the array is 2 (both 1 and 2 appear twice), and the shortest contiguous subarray with degree 2 is [2, 2], which has length 2.
Input: nums = [1, 2, 2, 3, 1, 4, 2]-List[int]
Output: 6-int
Explanation: The degree of the array is 3 (value 2 appears three times). To include all three 2s, the smallest subarray runs from the first 2 to the last 2, [2, 2, 3, 1, 4, 2], with length 6.
Input: nums = [5]-List[int]
Output: 1-int
Explanation: The degree is 1 (only one element), and any single-element subarray has degree 1, so the minimum length is 1.
Input: nums = [2, 1, 1, 2, 1, 3, 3, 3]-List[int]
Output: 3-int
Explanation: The degree is 3 (values 1 and 3 each appear three times). The shortest subarray achieving degree 3 is for value 3, [3, 3, 3], with length 3 (shorter than the span covering all 1s).
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: