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 ;
13
4+ // tag renovizee 1week unresolved
25// https://github.com/DaleStudy/leetcode-study/issues/240
36// https://leetcode.com/problems/longest-consecutive-sequence/
47class 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+ //-------------------------------------------------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments