<p>Given a non-empty array of non-negative integers <code>nums</code>, the <b>degree</b> of this array is defined as the maximum frequency of any one of its elements.</p>

<p>Your task is to find the smallest possible length of a (contiguous) subarray of <code>nums</code>, that has the same degree as <code>nums</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
The degree is 3 because the element 2 is repeated 3 times.
So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums.length</code> will be between 1 and 50,000.</li>
	<li><code>nums[i]</code> will be an integer between 0 and 49,999.</li>
</ul>

class Solution:
    def findShortestSubArray(self, nums: List[int]) -> int:

        if len(nums) < 2:
            return len(nums)

        degree = 0
        counts = {}
        curCounts = {}
        for i in range(len(nums)):
            if not nums[i] in counts:
                counts[nums[i]] = 1
                curCounts[nums[i]] = 0
            else:
                counts[nums[i]] += 1
            degree = max(degree, counts[nums[i]])

        if degree < 2:
            return degree

        minInterval = float('inf')
        startPointer = 0
        endPointer = 0
        curCounts[nums[endPointer]] += 1
        curAtMax = 0

        while endPointer < len(nums)-1:
            if curAtMax == 0:
                endPointer += 1
                curCounts[nums[endPointer]] += 1
                if curCounts[nums[endPointer]] == degree:
                    curAtMax = 1
            else:
                if curCounts[nums[startPointer]] == degree:
                    curAtMax = 0 
                curCounts[nums[startPointer]] -= 1
                startPointer += 1

            if curAtMax == 1:
                minInterval = min(minInterval, endPointer - startPointer + 1)
        while curAtMax == 1 and startPointer < len(nums):
            minInterval = min(minInterval, endPointer - startPointer + 1)

            if curCounts[nums[startPointer]] == degree:
                curAtMax = 0
            curCounts[nums[startPointer]] -= 1
            startPointer += 1

        return minInterval
            
    def findShortestSubArray(self, nums: List[int]) -> int:

        if len(nums) < 2:
            return len(nums)

        degree = 0
        counts = {}
        intervals = {}
        for i in range(len(nums)):
            if not nums[i] in counts:
                counts[nums[i]] = 1
                intervals[nums[i]] = [i,i]
            else:
                counts[nums[i]] += 1
                intervals[nums[i]][1] = i
            degree = max(degree, counts[nums[i]])

        if degree < 2:
            return degree
        minInterval = float('inf')
        for num, count in counts.items():
            if count == degree:
                minInterval = min(minInterval, intervals[num][1] - intervals[num][0] + 1)
        return minInterval


















        largestNums = set()
        for value, count in counts.items():
            if count == degree:
                degreeMatching += 1
                largestNums.add(value)
    
        
        def findLargest(startPointer, endPointer, largestNums):
            maxFromOthers = 0

            while endPointer > startPointer:
                endChar = nums[endPointer]
                startChar = nums[startPointer]

                if not endChar in largestNums:
                    endPointer -= 1
                elif not startChar in largestNums:
                    startPointer += 1
                elif len(largestNums) > 1:
                    #print(largestNums, len(largestNums))
                    if startChar == endChar:
                        startChar += 1
                        endPointer -= 1
                        largestNums.remove(endChar)
                        #print(largestNums, len(largestNums))
                    else:
                        withoutEnd = largestNums.copy()
                        withoutEnd.remove(endChar)
                        largestNums.remove(startChar)
                        return min(findLargest(startPointer, endPointer-1, withoutEnd), findLargest(startPointer+1, endPointer, largestNums))
                else:
                    #print(startPointer, endPointer)
                    return (endPointer - startPointer) + 1
            return 1
        endPointer, startPointer = len(nums)-1, 0
        return findLargest(startPointer, endPointer, largestNums)

        
18100