uncommited problems
This commit is contained in:
30
longest-consecutive-sequence/sol.go
Normal file
30
longest-consecutive-sequence/sol.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
func longestConsecutive(nums []int) (max int) {
|
||||
exists := make(map[int]bool, len(nums))
|
||||
for _, n := range nums {
|
||||
exists[n] = true
|
||||
}
|
||||
done := make(map[int]bool, len(nums))
|
||||
for _, n := range nums {
|
||||
if _, ok := done[n]; !ok {
|
||||
done[n] = true
|
||||
l := 1
|
||||
for exists[n+1] {
|
||||
l++
|
||||
n++
|
||||
done[n] = true
|
||||
}
|
||||
n -= l - 1
|
||||
for exists[n-1] {
|
||||
l++
|
||||
n--
|
||||
done[n] = true
|
||||
}
|
||||
if l > max {
|
||||
max = l
|
||||
}
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
Reference in New Issue
Block a user