File tree Expand file tree Collapse file tree
longest-consecutive-sequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+
6+ /*
7+ TC: O(n) -> for의 skip 부분을 while loop 분으로 채워짐
8+ SC: O(n) => Set 사용
9+ */
10+ var longestConsecutive = function ( nums ) {
11+ const numsSet = new Set ( nums ) ;
12+ let seq = 0 ;
13+
14+ for ( num of numsSet ) {
15+ if ( numsSet . has ( num - 1 ) ) continue ;
16+
17+ let next = num + 1 ;
18+ let currentSeq = 1 ;
19+
20+ while ( numsSet . has ( next ) ) {
21+ currentSeq ++ ;
22+ next ++ ;
23+ }
24+
25+ seq = Math . max ( seq , currentSeq ) ;
26+ }
27+
28+ return seq ;
29+ } ;
30+
31+ /*
32+ memory out
33+ */
34+ // var longestConsecutive = function(nums) {
35+ // if (nums.length <= 1) return nums.length;
36+
37+ // const dedupeNums = new Set(nums);
38+
39+ // let maxValue = -Infinity;
40+ // let minValue = Infinity;
41+ // dedupeNums.forEach((num) => {
42+ // maxValue = Math.max(maxValue, num);
43+ // minValue = Math.min(minValue, num);
44+ // });
45+
46+ // const markers = Array.from({ length: maxValue - minValue + 1 }, () => false);
47+ // let maxSequence = 0;
48+ // let currentSequence = 0;
49+
50+ // dedupeNums.forEach((num) => {
51+ // markers[num - minValue] = true;
52+ // });
53+
54+ // for (let i = 0; i < markers.length; i++) {
55+ // if (markers[i]) currentSequence++;
56+ // else {
57+ // maxSequence = Math.max(maxSequence, currentSequence);
58+ // currentSequence = 0;
59+ // }
60+ // }
61+
62+ // return Math.max(maxSequence, currentSequence);
63+ // };
You can’t perform that action at this time.
0 commit comments