Python solution with DP and set


  • 0
    D

    Just started learning coding. Hello neighbors ^_^

    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        
        nsorted=list(set(nums))
        nsorted.sort()
        m=len(nsorted)
        if m<=1:
            return m
        
        dp=[1]
        for i in range(1,m):
            if nsorted[i]==nsorted[i-1]+1:
                dp.append(dp[-1]+1)
            else:
                dp.append(1)
        return max(dp)

Log in to reply
 

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