Skip to content

Commit 340188b

Browse files
authored
improve comments on longest consecutive sequence
1 parent 161c76b commit 340188b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

longest-consecutive-sequence/samcho0608.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22
import java.util.Set;
33

44
class Solution {
5+
// Problem:
56
// * nums is unsorted
67
// * return: length of longest consecutive elements sequence
78
// * req: O(N) time
9+
// Solution:
10+
// * Time Complexity: O(N)
11+
// * Space Complexity: O(N)
812
public int longestConsecutive(int[] nums) {
13+
// Time Complexity: O(N)
14+
// Space Complexity: O(N)
915
Set<Integer> uniq = new HashSet<>();
10-
11-
// O(N)
1216
for(int num : nums) {
1317
uniq.add(num);
1418
}
1519

16-
// O(N)
20+
// Time Complexity: O(N)
21+
// * nested loop but is O(N) due to skipping non-root element
1722
int maxLen = 0;
1823
for(int num : uniq) {
19-
// 기존에 시작된 consecutive sequence가 있으면 스킵
24+
// skip if num isn't the root(aka first number in the sequence)
2025
if(uniq.contains(num - 1)) continue;
2126

2227
// count till end of consecutive sequence

0 commit comments

Comments
 (0)