File tree Expand file tree Collapse file tree 5 files changed +140
-0
lines changed Expand file tree Collapse file tree 5 files changed +140
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 배열의 원소 조합(중복 허용)의 합이 target인 모든 경우를 반환하는 함수
3
+ * @param {number[] } candidates
4
+ * @param {number } target
5
+ * @return {number[][] }
6
+ */
7
+ const combinationSum = function ( candidates , target ) {
8
+ const sortedCandidates = candidates . filter ( ( x ) => x <= target ) . sort ( ( a , b ) => Number ( a ) - Number ( b ) ) ;
9
+ const answer = [ ] ;
10
+
11
+ if ( sortedCandidates . length === 0 ) {
12
+ return answer ;
13
+ }
14
+
15
+ function search ( currentIdx , combination , total ) {
16
+ if ( total === target ) {
17
+ answer . push ( [ ...combination ] ) ; // 배열 자체를 넣으면 참조 때문에 값이 변경되므로, 복사해서 넣어야 함
18
+ return ;
19
+ }
20
+
21
+ if ( total > target ) {
22
+ return ; // backtracking
23
+ }
24
+
25
+ combination . push ( sortedCandidates [ currentIdx ] ) ;
26
+ search ( currentIdx , combination , total + sortedCandidates [ currentIdx ] ) ;
27
+ combination . pop ( ) ;
28
+
29
+ if ( total + sortedCandidates [ currentIdx ] > target ) {
30
+ return ; // backtracking
31
+ }
32
+
33
+ if ( currentIdx + 1 < sortedCandidates . length ) {
34
+ search ( currentIdx + 1 , combination , total ) ;
35
+ }
36
+ }
37
+
38
+ search ( 0 , [ ] , 0 ) ;
39
+ return answer ;
40
+ } ;
41
+
42
+ // t: target
43
+ // 시간복잡도: O(2^t)
44
+ // 공간복잡도: O(t)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 문자열을 복호화하는 경우의 수를 반환하는 함수
3
+ * @param {string } s
4
+ * @return {number }
5
+ */
6
+ const numDecodings = function ( s ) {
7
+ const dp = { } ;
8
+
9
+ function decode ( idx ) {
10
+ if ( s [ idx ] === '0' ) {
11
+ return 0 ;
12
+ }
13
+
14
+ if ( idx === s . length ) {
15
+ return 1 ;
16
+ }
17
+
18
+ if ( dp [ idx ] ) {
19
+ return dp [ idx ] ;
20
+ }
21
+
22
+ let result = 0 ;
23
+ result += decode ( idx + 1 ) ; // 현재 문자만 쓰는 경우: 다음 문자부터 탐색
24
+ if ( s [ idx + 1 ] && Number ( s [ idx ] + s [ idx + 1 ] ) <= 26 ) {
25
+ result += decode ( idx + 2 ) ; // 현재 문자와 다음 문자 붙여서 쓰는 경우: 다다음 문자부터 탐색
26
+ }
27
+
28
+ dp [ idx ] = result ;
29
+ return result ;
30
+ }
31
+
32
+ return decode ( 0 ) ;
33
+ } ;
34
+
35
+ // 시간복잡도: O(n) (메모이제이션 안하면 매 인덱스마다 최대 2개의 하위 호출이 발생하여 O(2^n))
36
+ // 공간복잡도: O(n)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 배열에서 원소의 합이 가장 큰 부분 배열의 합을 반환하는 함수
3
+ * @param {number[] } nums
4
+ * @return {number }
5
+ */
6
+ const maxSubArray = function ( nums ) {
7
+ let max = nums [ 0 ] ;
8
+ let subSum = 0 ;
9
+
10
+ nums . forEach ( ( num ) => {
11
+ subSum = Math . max ( subSum + num , num ) ;
12
+
13
+ if ( max < subSum ) {
14
+ max = subSum ;
15
+ }
16
+ } ) ;
17
+
18
+ return max ;
19
+ } ;
20
+
21
+ // 시간복잡도: O(n)
22
+ // 공간복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 숫자를 2진수로 표현했을 때 비트가 1인 개수를 반환하는 함수
3
+ * @param {number } n
4
+ * @return {number }
5
+ */
6
+ const hammingWeight = function ( n ) {
7
+ const binary = n . toString ( 2 ) ;
8
+ return Array . from ( binary ) . filter ( ( bit ) => bit == 1 ) . length ;
9
+ } ;
10
+
11
+ // 시간복잡도: O(n)
12
+ // 공간복잡도: O(n)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 문자열이 조건을 만족하는 회문인지 여부를 반환하는 함수
3
+ * @param {string } s
4
+ * @return {boolean }
5
+ */
6
+ const isPalindrome = function ( s ) {
7
+ const filtered = Array . from ( s . toLowerCase ( ) ) . reduce ( ( str , char ) => {
8
+ return isAlphanumeric ( char ) ? str + char : str ;
9
+ } , '' ) ;
10
+
11
+ for ( let left = 0 , right = filtered . length - 1 ; left < right ; left ++ , right -- ) {
12
+ if ( filtered [ left ] !== filtered [ right ] ) {
13
+ return false ;
14
+ }
15
+ }
16
+
17
+ return true ;
18
+ } ;
19
+
20
+
21
+ function isAlphanumeric ( char ) {
22
+ return char !== ' ' && ( ( 'a' <= char && char <= 'z' ) || ! Number . isNaN ( Number ( char ) ) ) ;
23
+ }
24
+
25
+ // 시간복잡도: O(n)
26
+ // 공간복잡도: O(n)
You can’t perform that action at this time.
0 commit comments