Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
1 | Input: nums = [1,2,2,3,1] |
Example 2:
1 | Input: nums = [1,2,2,3,1,4,2] |
Constraints:
1 | nums.length will be between 1 and 50,000. |
Approach
1 | Memorise where the element first appears and last apears and how many times the element appears. Find smallest possible length of a subarray by going through the hash table again. |
Algorithm
1 | 1. Go through nums to find where the element first appears and last apears and how many times the element appears. |
Implementation
1 | int findShortestSubArray(int* nums, int numsSize){ |