Python easy O(n) time & space


  • 0
    W

    '''

    def findShortestSubArray(self, nums):
        from collections import Counter
        d = Counter(nums)
        longest = float("inf")
        md = dict()
        max_count = max(d.values())
        for i in d:
            if d[i] == max_count:
                md[i] = [0, 0] #count, start
        for i, j in enumerate(nums):
            if j in md:
                temp_list = md[j]
                if temp_list[0] == 0:
                    temp_list[1] = i
                temp_list[0] += 1
                if temp_list[0] == max_count and i - temp_list[1] + 1 < longest:
                    longest = i - temp_list[1] + 1
        return longest
    

    '''


Log in to reply
 

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