Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def longestConsecutive(self, nums):
- if not nums:
- return 0
- nums.sort()
- longest_streak = 0
- current_streak = 1
- for index, num in enumerate(nums[:-1]):
- # an important step here would be to avoid duplicates
- if (nums[index] != nums[index+1]):
- if (nums[index] + 1 == nums[index+1]):
- current_streak += 1
- else:
- longest_streak = max(current_streak, longest_streak)
- current_streak = 1
- # very important step here since there could be cases
- # where current streak could be every increasing and
- # longest streak could not get a chance to be assigned
- longest_streak = max(current_streak, longest_streak)
- return longest_streak
Add Comment
Please, Sign In to add comment