File tree Expand file tree Collapse file tree 3 files changed +126
-0
lines changed Expand file tree Collapse file tree 3 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {boolean }
4+ *
5+ * 시간복잡도 계산
6+ * 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가
7+ * => O(n)
8+ *
9+ * 공간복잡도 계산
10+ * indices가 nums에 비례해서 할당
11+ * 상수 i 할당
12+ * => O(n)
13+ */
14+ var containsDuplicate = function ( nums ) {
15+ let indices = { } ;
16+ nums . forEach ( ( num , index ) => {
17+ indices [ num ] = index ;
18+ } ) ;
19+
20+ for ( let i = 0 ; i < nums . length ; i ++ ) {
21+ if ( nums [ i ] in indices && indices [ nums [ i ] ] !== i ) return true ;
22+ }
23+
24+ return false ;
25+ } ;
26+
27+ /**
28+ * @param {number[] } nums
29+ * @return {boolean }
30+ *
31+ * 시간복잡도 계산
32+ * Set의 size속성은 입력 크기와 관계없이 일정한 시간이 걸림
33+ * => O(1)
34+ *
35+ * 공간복잡도 계산
36+ * indices가 nums에 비례해서 할당
37+ * 상수 i 할당
38+ * => O(n)
39+ */
40+ var containsDuplicate = function ( nums ) {
41+ const indices = new Set ( nums ) ;
42+
43+ if ( indices . size !== nums . length ) return true ;
44+
45+ return false ;
46+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } k
4+ * @return {number[] }
5+ *
6+ * 시간복잡도 계산
7+ * for loop가 O(n)
8+ * reduce가 O(n)
9+ * sort가 O(n log n)
10+ * => O(n log n)
11+ */
12+ var topKFrequent = function ( nums , k ) {
13+ let result = [ ] ;
14+ for ( let i = 0 ; i < nums . length ; i ++ ) {
15+ if ( result . find ( ( el ) => el . key == nums [ i ] ) ) continue ;
16+ const count = nums . reduce ( ( cnt , el ) => cnt + ( nums [ i ] === el ) , 0 ) ;
17+ result . push ( { key : nums [ i ] , value : count } ) ;
18+ }
19+
20+ return result
21+ . sort ( ( a , b ) => b . value - a . value )
22+ . slice ( 0 , k )
23+ . map ( ( el ) => el . key ) ;
24+ } ;
25+
26+ /**
27+ * Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
28+ */
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } target
4+ * @return {number[] }
5+ *
6+ * 시간복잡도 계산
7+ * 입력 크기(n)가 커질 때 연산 횟수가 n^2에 비례해서 증가
8+ * => O(n^2)
9+ *
10+ * 공간복잡도 계산
11+ * 상수 i 할당
12+ * 상수 j 할당
13+ * => o(1)
14+ */
15+ var twoSum = function ( nums , target ) {
16+ for ( let i = 0 ; i < nums . length ; i ++ ) {
17+ for ( let j = 0 ; j < nums . length ; j ++ ) {
18+ if ( i === j ) continue ;
19+ if ( nums [ i ] + nums [ j ] === target ) return [ i , j ] ;
20+ }
21+ }
22+ } ;
23+
24+ /**
25+ * @param {number[] } nums
26+ * @param {number } target
27+ * @return {number[] }
28+ *
29+ * 시간복잡도 계산
30+ * 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가
31+ * => O(n)
32+ *
33+ * 공간복잡도 계산
34+ * result가 nums에 비례해서 할당
35+ * 상수 i, findNum 할당
36+ * => O(n)
37+ */
38+ var twoSum = function ( nums , target ) {
39+ let result = { } ;
40+
41+ nums . forEach ( ( num , index ) => {
42+ result [ num ] = index ;
43+ } ) ;
44+
45+ for ( let i = 0 ; i < nums . length ; i ++ ) {
46+ const findNum = target - nums [ i ] ;
47+
48+ if ( findNum in result && result [ findNum ] !== i ) {
49+ return [ i , result [ findNum ] ] ;
50+ }
51+ }
52+ } ;
You can’t perform that action at this time.
0 commit comments