Advertisement
aero2146

Longest Consecutive Sequence

Apr 10th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. class Solution:
  2.     def longestConsecutive(self, nums):
  3.         longest_streak = 0
  4.         num_set = set(nums)
  5.  
  6.         for num in num_set:
  7.             if num - 1 not in num_set:
  8.                 current_num = num
  9.                 current_streak = 1
  10.  
  11.                 while current_num + 1 in num_set:
  12.                     current_num += 1
  13.                     current_streak += 1
  14.  
  15.                 longest_streak = max(longest_streak, current_streak)
  16.  
  17.         return longest_streak
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement