smj007

Untitled

Jun 18th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Solution:
  2.     def longestConsecutive(self, nums):
  3.        
  4.         longest_streak = 0
  5.         num_set = set(nums)
  6.        
  7.         for i in range(len(nums)):
  8.             # check if the current element is the first elements
  9.             # this comes from a DP approach and  done to avoid repeat iterations
  10.             if (nums[i] - 1) not in num_set:
  11.                
  12.                 current_streak = 1
  13.                 while((nums[i] + current_streak) in num_set):
  14.                     current_streak += 1
  15.                    
  16.                 longest_streak = max(current_streak, longest_streak)
  17.                
  18.         return longest_streak
Advertisement
Add Comment
Please, Sign In to add comment