Skip to content

Commit b8140d5

Browse files
committed
feat(leetcode/128): Solve Longest Consecutive Sequence (Medium)
1 parent f13c67c commit b8140d5

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
13

4+
// tag renovizee 1week unresolved
25
// https://github.com/DaleStudy/leetcode-study/issues/240
36
// https://leetcode.com/problems/longest-consecutive-sequence/
47
class Solution {
58
public int longestConsecutive(int[] nums) {
9+
// 시간복잡도 : O(n)
10+
// 공간복잡도 : O(n)
611

7-
return 1;
12+
Set<Integer> numSet = new HashSet<>();
13+
for (int num : nums) {
14+
numSet.add(num);
15+
}
816

17+
int maxCount = 0;
18+
for (int num : nums) {
19+
if (numSet.contains(num - 1)) continue;
20+
int currentCount = 1;
21+
while (numSet.contains(num + currentCount)) {
22+
currentCount++;
23+
}
24+
maxCount = Math.max(maxCount, currentCount);
25+
}
26+
return maxCount;
927
}
1028
}
29+
30+
31+
//-------------------------------------------------------------------------------------------------------------
32+
// 기본 문법 피드백
33+
// 1) Set<Integer> numSet = new HashSet<>();
34+
// 2) Math 활용 Math.max(maxCount, currentCount);
35+
//-------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)