forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathohgyulim.java
More file actions
32 lines (28 loc) · 751 Bytes
/
ohgyulim.java
File metadata and controls
32 lines (28 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
class Solution {
/* 시간 복잡도: O(N)
* - for 루프: O(N) O(N)
* - HashSet(add, contains): O(1)
*
* 공간 복잡도: O(N), HashSet에 n개
*/
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int answer = 0;
for (int num : set) {
if (!set.contains(num - 1)) {
int cur = num;
int count = 1;
while (set.contains(cur + 1)) {
cur += 1;
count += 1;
}
answer = Math.max(answer, count);
}
}
return answer;
}
}