Skip to content

Commit ddc9916

Browse files
committed
Longest Consecutive Sequence
1 parent cc47d80 commit ddc9916

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
int output = 0;
6+
Set<Integer> set = new HashSet<>();
7+
8+
for (int num : nums) set.add(num);
9+
10+
for (int num : nums) {
11+
int count = 1;
12+
if (!set.contains(num - count)){
13+
while (set.contains(num + count)) {
14+
count += 1;
15+
}
16+
}
17+
output = Math.max(output, count);
18+
}
19+
return output;
20+
}
21+
}

0 commit comments

Comments
 (0)