Simple Python Solution


  • 0
    M
        def findShortestSubArray(self, nums):
            from collections import defaultdict
            
            freq = defaultdict(int)
            dist = defaultdict(list)
            max_freq = float('-Inf')
            for i in xrange(len(nums)):
                freq[nums[i]] += 1
                dist[nums[i]] += i,
    
                max_freq = max(max_freq, freq[nums[i]])
            
            min_degree = float('Inf')
            for num in freq:
                if freq[num] == max_freq:
                    min_degree = min(min_degree, dist[num][-1] - dist[num][0] + 1)
            
            return min_degree
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.