File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
1
3
4
+ // tag renovizee 1week unresolved
2
5
// https://github.com/DaleStudy/leetcode-study/issues/240
3
6
// https://leetcode.com/problems/longest-consecutive-sequence/
4
7
class Solution {
5
8
public int longestConsecutive (int [] nums ) {
9
+ // 시간복잡도 : O(n)
10
+ // 공간복잡도 : O(n)
6
11
7
- return 1 ;
12
+ Set <Integer > numSet = new HashSet <>();
13
+ for (int num : nums ) {
14
+ numSet .add (num );
15
+ }
8
16
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 ;
9
27
}
10
28
}
29
+
30
+
31
+ //-------------------------------------------------------------------------------------------------------------
32
+ // 기본 문법 피드백
33
+ // 1) Set<Integer> numSet = new HashSet<>();
34
+ // 2) Math 활용 Math.max(maxCount, currentCount);
35
+ //-------------------------------------------------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments