We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc47d80 commit ddc9916Copy full SHA for ddc9916
longest-consecutive-sequence/TonyKim9401.java
@@ -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