Advertisement
lazar955

Untitled

Oct 22nd, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.46 KB | None | 0 0
  1. func longestConsecutive(nums []int) int {
  2.     numSet:=map[int]bool{}
  3.     for _,v:=range nums {
  4.         numSet[v] = true
  5.     }
  6.     longest := 0
  7.  
  8.    for num:=range numSet {
  9.        if numSet[num - 1] {
  10.            continue
  11.        }
  12.        cur :=num
  13.        for numSet[cur+1] {
  14.            cur++
  15.        }
  16.        longest = max(longest,cur - num + 1)
  17.    }
  18.     return longest
  19. }
  20. func max(a, b int)int{
  21.     if a > b{
  22.         return a
  23.     }
  24.     return b
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement