Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Why O(n) ?
- Since each element will be visited only twice, once in for loop and once in the while loop of its root (i.e if seq. is 2 3 4 then root of all of them is 2)
- Thus asymptotically linear.
- """
- class Solution:
- def longestConsecutive(self, nums: List[int]) -> int:
- lookup = set(nums)
- ans = 0
- for e in nums:
- if e-1 in lookup:
- continue
- cnt = 0
- while e in lookup:
- cnt += 1
- e += 1
- ans = max(ans, cnt)
- return ans
Add Comment
Please, Sign In to add comment