File tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +136
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간 복잡도: O(n) => 배열을 한 번만 순회하므로
2
+ // 공간 복잡도: O(n) => 최악의 경우 모든 요소를 객체에 저장하므로
3
+ const containsDuplicate = ( nums ) => {
4
+ const indices = { } ;
5
+
6
+ // 배열을 한 번 순회
7
+ for ( let i = 0 ; i < nums . length ; i += 1 ) {
8
+ const num = nums [ i ] ;
9
+
10
+ // 아직 등장하지 않은 숫자라면 객체에 기록
11
+ if ( ! indices [ num ] ) {
12
+ indices [ num ] = 1 ;
13
+ } else {
14
+ // 이미 등장한 숫자라면 중복이므로 true 반환
15
+ return true ;
16
+ }
17
+ }
18
+
19
+ // 중복이 없으면 false 반환
20
+ return false ;
21
+ } ;
Original file line number Diff line number Diff line change
1
+ // 시간 복잡도: O(n) 한 번의 루프만 돎
2
+ // 공간 복잡도: O(n) nums와 같은 길이의 dp배열 생성
3
+
4
+ const rob = ( nums ) => {
5
+ if ( nums . length === 0 ) return 0 ;
6
+ if ( nums . length === 1 ) return nums [ 0 ] ;
7
+
8
+ const dp = Array ( nums . length - 1 ) . fill ( 0 ) ;
9
+
10
+ dp [ 0 ] = nums [ 0 ] ;
11
+ dp [ 1 ] = Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
12
+
13
+ for ( let i = 2 ; i < nums . length ; i += 1 ) {
14
+ dp [ i ] = Math . max ( nums [ i ] + dp [ i - 2 ] , dp [ i - 1 ] ) ;
15
+ }
16
+
17
+ return dp [ nums . length - 1 ] ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간복잡도: O(n)
3
+ * - set.has() => O(1)
4
+ * - while() => 최악의 경우 O(n)
5
+ * 공간복잡도: new Set() => O(n)
6
+ */
7
+ const longestConsecutive = ( nums ) => {
8
+ // 빈 배열일 때 0 리턴
9
+ if ( nums . length === 0 ) return 0 ;
10
+
11
+ // 중복 제거
12
+ const set = new Set ( nums ) ;
13
+
14
+ // 연속되는 숫자의 길이를 넣을 변수 설정
15
+ let longest = 0 ;
16
+
17
+ set . forEach ( ( v ) => {
18
+ // 일단 연속되는 배열에서는 가장 최솟값을 찾음
19
+ if ( ! set . has ( v - 1 ) ) {
20
+ let cnt = 1 ;
21
+ // 연속되는 배열에서 가장 긴 배열을 저장
22
+ while ( set . has ( v + cnt ) ) {
23
+ cnt += 1 ;
24
+ }
25
+ longest = Math . max ( longest , cnt ) ;
26
+ }
27
+ } ) ;
28
+
29
+ return longest ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간 복잡도: O(n)
3
+ * - nums 순회하며 map 생성 O(n)
4
+ * - map 순회하며 bucket 생성 O(n)
5
+ * - bucket 순회하며 결과 찾음 O(n)
6
+ *
7
+ * 공간 복잡도: O(n)
8
+ * - map O(n)
9
+ * - bucket O(n)
10
+ * - result k
11
+ */
12
+ const topKFrequent = ( nums , k ) => {
13
+ // nums 요소 : 요소의 갯수
14
+ const map = { } ;
15
+ // 요소의 갯수 : Set{ nums 요소 }
16
+ const bucket = [ ] ;
17
+
18
+ const result = [ ] ;
19
+
20
+ for ( let i = 0 ; i < nums . length ; i += 1 ) {
21
+ if ( map [ nums [ i ] ] ) {
22
+ map [ nums [ i ] ] += 1 ;
23
+ } else {
24
+ map [ nums [ i ] ] = 1 ;
25
+ }
26
+ }
27
+
28
+ for ( let [ num , freq ] of Object . entries ( map ) ) {
29
+ if ( bucket [ freq ] ) {
30
+ bucket [ freq ] = bucket [ freq ] . add ( num ) ;
31
+ } else {
32
+ bucket [ freq ] = new Set ( ) . add ( num ) ;
33
+ }
34
+ }
35
+
36
+ for ( let i = bucket . length - 1 ; i >= 0 ; i -= 1 ) {
37
+ if ( bucket [ i ] ) {
38
+ // string을 number로 변환
39
+ const value = Array . from ( bucket [ i ] , Number ) ;
40
+ result . push ( ...value ) ;
41
+ }
42
+ if ( result . length === k ) break ;
43
+ }
44
+
45
+ return result ;
46
+ } ;
Original file line number Diff line number Diff line change
1
+ // 배열을 한 번 도니 시간 복잡도 O(n)
2
+ // 최악의 경우 nums의 크기 배열만큼 증가하므로 공간 복잡도는 O(n)
3
+
4
+ const twoSum = ( nums , target ) => {
5
+ // {값: 인덱스} 형태로 저장
6
+ const indicies = { } ;
7
+
8
+ for ( let i = 0 ; i < nums . length ; i += 1 ) {
9
+ // 타겟값에서 현재 가리키는 숫자를 뺀 값을 저장
10
+ const complement = target - nums [ i ] ;
11
+
12
+ // complement가 indicies안에 존재하면 해당 값을 반환
13
+ if ( complement in indicies ) {
14
+ const j = indicies [ complement ] ;
15
+ return [ j , i ] ;
16
+ }
17
+ // 존재 하지 않으면 값과 인덱스 형태로 저장 ex> { 11: 0, 15: 1, 2: 2 }
18
+ indicies [ nums [ i ] ] = i ;
19
+ console . log ( indicies ) ;
20
+ }
21
+ } ;
You can’t perform that action at this time.
0 commit comments