3 solutions - Clean & Concise
✔️ Solution 1: Sorting then check consecutive elementsComplexity:
Time: O(NlogN), where N is length of nums array
Space: O(sorting(N)), HeapSort can take O(1) i
**✔️ Solution 1: Sorting then check consecutive elements**
```python
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        if not nums:
            return 0

        nums.sort()
        ans = 1
        consecutive_size = 1
        for i in range(1, len(nums)):
            if nums[i-1] == nums[i]: continue
            if nums[i-1] + 1 == nums[i]:
                consecutive_size += 1
            else:
                ans = max(ans, consecutive_size)
                consecutive_size = 1

        ans = max(ans, consecutive_size)
        return ans
```
Complexity:
- Time: `O(NlogN)`, where `N` is length of `nums` array
- Space: `O(sorting(N))`, HeapSort can take O(1) in space complexity.

---
**✔️ Solution 2: Top down DP**
```python
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        nums = set(nums)
        
        @lru_cache(None)
        def dp(num):
            if num not in nums:
                return 0
            
            return dp(num+1) + 1
        
        ans = 0
        for num in nums:
            ans = max(ans, dp(num))
        return ans
```
Complexity:
- Time: `O(N)`, where `N` is length of `nums` array
- Space: `O(N)`

---
**✔️ Solution 3: Find the left most of the consecutive subsequence then expand the right side**
```python
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        ans = 0
        nums = set(nums)
        for num in nums:
            if num - 1 in nums: continue # num is not the left most number
            size = 1
            while num + 1 in nums:
                num += 1
                size += 1
            ans = max(ans, size)
        return ans
```
Complexity:
- Time: `O(N)`, where `N` is length of `nums` array
- Space: `O(N)`
['Python', 'Python3']