File tree Expand file tree Collapse file tree 5 files changed +120
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +120
-0
lines changed Original file line number Diff line number Diff line change
1
+ function containsDuplicate ( nums : number [ ] ) : boolean {
2
+ const set = new Set ( nums ) ;
3
+ const setSize = set . size ;
4
+ const numsLength = nums . length ;
5
+
6
+ return setSize !== numsLength ? true : false ;
7
+
8
+ // Big O
9
+ // 시간 복잡도: O(n) 한번씩 순회하면서 확인
10
+ // 공간 복잡도: O(n) Set을 사용해 배열 크기만큼 메모리가 더 사용됨
11
+ } ;
Original file line number Diff line number Diff line change
1
+ function rob ( nums : number [ ] ) : number {
2
+ // 첫시도: 아예 떨어져있는 경우를 고려하지 않음 (통과 안됨)
3
+ // let evenSum = 0;
4
+ // let oddSum = 0;
5
+
6
+ // for (let i=0; i<nums.length; i++) {
7
+ // if (i % 2 === 0) {
8
+ // evenSum += nums[i];
9
+ // } else {
10
+ // oddSum += nums[i];
11
+ // }
12
+ // }
13
+
14
+ // return Math.max(evenSum, oddSum);
15
+
16
+ // 시간복잡도 O(n), 공간복잡도 O(1)
17
+ if ( nums . length === 0 ) return 0 ;
18
+ if ( nums . length === 1 ) return nums [ 0 ] ;
19
+
20
+ let prev = nums [ 0 ] ;
21
+ let max = Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
22
+
23
+ for ( let i = 2 ; i < nums . length ; i ++ ) {
24
+ const current = Math . max ( max , prev + nums [ i ] ) ;
25
+ prev = max ;
26
+ max = current ;
27
+ }
28
+
29
+ return max ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ function longestConsecutive ( nums : number [ ] ) : number {
2
+ // 첫 시도: sort때문에 시간 복잡도가 O(n log n)라 O(n)에 풀어야 한다는 요구사항 만족하지 못함, 공간복잡도는 O(n)
3
+ // if (nums.length === 0) return 0;
4
+ // const sorted = [...new Set(nums)].sort((a, b) => a-b);
5
+ // let max = 1;
6
+ // let current = 1;
7
+
8
+ // for (let i=0; i<nums.length; i++) {
9
+ // console.log(sorted[i])
10
+ // if (sorted[i+1] - sorted[i] === 1) {
11
+ // current += 1;
12
+ // } else {
13
+ // current = 1;
14
+ // }
15
+
16
+ // max = Math.max(max, current);
17
+ // }
18
+
19
+ // return max;
20
+
21
+ // 두번째 시도: 시간, 공간복잡도 O(n)
22
+ if ( nums . length === 0 ) return 0 ;
23
+
24
+ const numSet = new Set ( nums ) ;
25
+ let current = 0 ;
26
+ let consecutive = 1 ;
27
+ let max = 1 ;
28
+
29
+ for ( let num of numSet ) {
30
+ if ( ! numSet . has ( num - 1 ) ) {
31
+ current = num ;
32
+ consecutive = 1 ;
33
+
34
+ while ( numSet . has ( current + 1 ) ) {
35
+ consecutive += 1 ;
36
+ current = current + 1 ;
37
+ }
38
+
39
+ max = Math . max ( consecutive , max ) ;
40
+ }
41
+ }
42
+
43
+ return max ;
44
+ } ;
Original file line number Diff line number Diff line change
1
+ function topKFrequent ( nums : number [ ] , k : number ) : number [ ] {
2
+ const map = new Map ( ) ;
3
+
4
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5
+ const num = nums [ i ]
6
+ map . set ( num , ( map . get ( num ) || 0 ) + 1 ) ;
7
+ }
8
+
9
+ return [ ...map . entries ( ) ]
10
+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
11
+ . slice ( 0 , k )
12
+ . map ( ( [ key ] ) => key ) ;
13
+ } ;
14
+
15
+ // 시간 복잡도: O(n log n)
16
+ // 공간 복잡도: O(n)
Original file line number Diff line number Diff line change
1
+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
2
+ // 첫 코드: 시간 복잡도가 O(n^2)로 개선 필요
3
+ // for (let i=0; i<nums.length-1; i++) {
4
+ // for (let j=1; j<nums.length; j++) {
5
+ // if (nums[i] + nums[j] === target) return [i, j];
6
+ // }
7
+ // }
8
+
9
+ // 시간 공간 모두 O(n)으로 만들기 위해 해시맵 사용
10
+ const map = new Map ( ) ;
11
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
12
+ const complement = target - nums [ i ] ;
13
+ if ( map . get ( complement ) !== undefined ) {
14
+ return [ map . get ( complement ) ! , i ] ;
15
+ } else {
16
+ map . set ( nums [ i ] , i ) ;
17
+ }
18
+ }
19
+ } ;
You can’t perform that action at this time.
0 commit comments