Skip to content

Commit bfd0d2c

Browse files
μž₯μž¬μ •μž₯μž¬μ •
authored andcommitted
Longest Consecutive Sequence
1 parent 3db966a commit bfd0d2c

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

β€Žlongest-consecutive-sequence/jaejeong1.javaβ€Ž

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,33 @@
44
class SolutionLongestConsecutiveSequence {
55

66
public int longestConsecutive(int[] nums) {
7-
// μ •λ ¬λ˜μ§€ μ•Šμ€ μ •μˆ˜ nums 배열이 μ£Όμ–΄μ§€λ©΄ κ°€μž₯ κΈ΄ 연속 μš”μ†Œ μ‹œν€€μŠ€ 길이λ₯Ό λ°˜ν™˜
8-
// O(N) μ‹œκ°„ λ‚΄ μ‹€ν–‰λ˜μ•Όν•¨
9-
// μ „λΆ€ ν•΄μ‹œλ§΅μ— λ•Œλ €λ„£κ³ , ν‚€λ₯Ό κΊΌλ‚΄ 연속 μš”μ†Œκ°€ μžˆλŠ”μ§€ ν™•μΈν•œλ‹€
10-
// 연속 μš”μ†Œκ°€ 있으면 answerλ₯Ό 1 μ¦κ°€μ‹œν‚€κ³ , 연속 μš”μ†ŒλŠ” μ œκ±°ν•œλ‹€
11-
// μ‹œκ°„λ³΅μž‘λ„: O(N), κ³΅κ°„λ³΅μž‘λ„: O(N)
12-
13-
Set<Integer> set = new HashSet<>();
14-
for (var num : nums) {
15-
set.add(num);
16-
}
17-
var answer = 0;
18-
for (var num : nums) {
19-
var length = 1;
20-
21-
if (set.contains(num-1)) {
22-
set.remove(num);
23-
var minusKey = num;
24-
while (set.contains(--minusKey)) {
25-
length++;
26-
set.remove(minusKey);
27-
}
7+
// μ‹œν€€μŠ€ 쑰회λ₯Ό O(1)에 μˆ˜ν–‰ κ°€λŠ₯ν•˜κ³ , 쀑볡은 λ¬΄μ‹œν•  수 μžˆλŠ” μ‘°κ±΄μ΄λ―€λ‘œ Set이 μ ν•©ν•œ 자료ꡬ쑰
8+
// μ‹œκ°„λ³΅μž‘λ„: O(N), κ³΅κ°„λ³΅μž‘λ„: O(N)
9+
Set<Integer> num_set = new HashSet<Integer>();
10+
for (int num : nums) {
11+
num_set.add(num);
2812
}
2913

30-
if (set.contains(num+1)) {
31-
set.remove(num);
32-
var plusKey = num;
33-
while (set.contains(++plusKey)) {
34-
length++;
35-
set.remove(plusKey);
36-
}
37-
}
14+
int longestSequence = 0;
15+
16+
// μ‹œκ°„λ³΅μž‘λ„: O(N)
17+
for (int num : num_set) {
18+
// μ‹œν€€μŠ€ 쀑간에 μžˆλŠ” μˆ«μžκ°€ μ•„λ‹Œ μ‹œμž‘ν•˜λŠ” 숫자λ₯Ό 찾음
19+
// μ‹œμž‘ν•˜λŠ” μˆ«μžλŠ” - 1 값이 Set에 없을 것
20+
if (!num_set.contains(num - 1)) {
21+
int currentNum = num;
22+
int currentLength = 1;
23+
// + 1 값이 Set에 μžˆλŠ” μ§€ ν™•μΈν•˜λ©΄μ„œ 증가
24+
while (num_set.contains(currentNum + 1)) {
25+
currentNum += 1;
26+
currentLength += 1;
27+
}
3828

39-
if (length > answer) {
40-
answer = length;
29+
// μˆœνšŒκ°€ λλ‚˜λ©΄ μ΅œλŒ€ μ‹œν€€μŠ€ 길이인지 ν™•μΈν•˜κ³  적용
30+
longestSequence = Math.max(longestSequence, currentLength);
31+
}
4132
}
42-
}
4333

44-
return answer;
34+
return longestSequence;
4535
}
4636
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
// μ‹œν€€μŠ€ 쑰회λ₯Ό O(1)에 μˆ˜ν–‰ κ°€λŠ₯ν•˜κ³ , 쀑볡은 λ¬΄μ‹œν•  수 μžˆλŠ” μ‘°κ±΄μ΄λ―€λ‘œ Set이 μ ν•©ν•œ 자료ꡬ쑰
4+
// μ‹œκ°„λ³΅μž‘λ„: O(N), κ³΅κ°„λ³΅μž‘λ„: O(N)
5+
Set<Integer> num_set = new HashSet<Integer>();
6+
for (int num : nums) {
7+
num_set.add(num);
8+
}
9+
10+
int longestSequence = 0;
11+
12+
// μ‹œκ°„λ³΅μž‘λ„: O(N)
13+
for (int num : num_set) {
14+
// μ‹œν€€μŠ€ 쀑간에 μžˆλŠ” μˆ«μžκ°€ μ•„λ‹Œ μ‹œμž‘ν•˜λŠ” 숫자λ₯Ό 찾음
15+
// μ‹œμž‘ν•˜λŠ” μˆ«μžλŠ” - 1 값이 Set에 없을 것
16+
if (!num_set.contains(num - 1)) {
17+
int currentNum = num;
18+
int currentLength = 1;
19+
// + 1 값이 Set에 μžˆλŠ” μ§€ ν™•μΈν•˜λ©΄μ„œ 증가
20+
while (num_set.contains(currentNum + 1)) {
21+
currentNum += 1;
22+
currentLength += 1;
23+
}
24+
25+
// μˆœνšŒκ°€ λλ‚˜λ©΄ μ΅œλŒ€ μ‹œν€€μŠ€ 길이인지 ν™•μΈν•˜κ³  적용
26+
longestSequence = Math.max(longestSequence, currentLength);
27+
}
28+
}
29+
30+
return longestSequence;
31+
}
32+
}

0 commit comments

Comments
Β (0)