Skip to content

Commit 3561a31

Browse files
authored
longest consecutive sequence solution
1 parent 5575f5f commit 3561a31

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func longestConsecutive(nums []int) int {
2+
ans := 0
3+
sort.Ints(nums)
4+
i := 0
5+
for i != len(nums) {
6+
j := i + 1
7+
k := 0
8+
for j != len(nums) {
9+
diff := nums[j] - nums[j - 1]
10+
if diff > 1 {
11+
break
12+
}
13+
j++
14+
if diff == 0 {
15+
k++
16+
}
17+
}
18+
ans = max(ans, j - i - k)
19+
i = j
20+
}
21+
return ans
22+
}

0 commit comments

Comments
 (0)