Python Solution,Hash table and 2 Pointers


  • 0
    Y
        def findShortestSubArray(self, A):
            """
            :type nums: List[int]
            :rtype: int
            """
            from collections import Counter
            n=len(A)
            mp=Counter(A)
            degree=0
            for key in mp:
                degree=max(degree,mp[key])
                
            i,j=0,0
            mp2={}
            res=n+1
            while j<n:
                mp2[A[j]]=mp2.get(A[j],0)+1
                if mp2[A[j]]==degree:
                    res=min(res,j-i+1)
                    while A[i]!=A[j]:
                        mp2[A[i]]-=1
                        i+=1
                    res=min(res,j-i+1)
                j+=1
            return res

Log in to reply
 

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