Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def longestConsecutive(self, nums):
- longest_streak = 0
- num_set = set(nums)
- for i in range(len(nums)):
- # check if the current element is the first elements
- # this comes from a DP approach and done to avoid repeat iterations
- if (nums[i] - 1) not in num_set:
- current_streak = 1
- while((nums[i] + current_streak) in num_set):
- current_streak += 1
- longest_streak = max(current_streak, longest_streak)
- return longest_streak
Advertisement
Add Comment
Please, Sign In to add comment