File tree 3 files changed +76
-0
lines changed
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간복잡도: O(2^n)
3
+ * 공간복잡도: O(target)
4
+ */
5
+
6
+ function combinationSum ( canditates , target ) {
7
+ const result = [ ] ;
8
+ const nums = [ ] ;
9
+
10
+ function dfs ( start , total ) {
11
+ if ( total > target ) return ;
12
+ if ( total === target ) result . push ( [ ...nums ] ) ;
13
+
14
+ // 중복제거를 위해 start로 시작
15
+ for ( let i = start ; i < canditates . length ; i += 1 ) {
16
+ num = canditates [ i ] ;
17
+ nums . push ( num ) ;
18
+ dfs ( i , total + num ) ;
19
+ nums . pop ( ) ;
20
+ }
21
+ }
22
+
23
+ // 시작 인덱스, 누적 합
24
+ dfs ( 0 , 0 ) ;
25
+
26
+ return result ;
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간 복잡도: O(n)
3
+ * 공간 복잡도: O(n)
4
+ */
5
+ var numDecodings = function ( s ) {
6
+ const memo = new Map ( ) ;
7
+ memo . set ( s . length , 1 ) ;
8
+
9
+ function dfs ( start ) {
10
+ if ( memo . has ( start ) ) {
11
+ return memo . get ( start ) ;
12
+ }
13
+
14
+ if ( s [ start ] === '0' ) {
15
+ memo . set ( start , 0 ) ;
16
+ } else if ( start + 1 < s . length && parseInt ( s . slice ( start , start + 2 ) ) < 27 ) {
17
+ memo . set ( start , dfs ( start + 1 ) + dfs ( start + 2 ) ) ;
18
+ } else {
19
+ memo . set ( start , dfs ( start + 1 ) ) ;
20
+ }
21
+
22
+ return memo . get ( start ) ;
23
+ }
24
+
25
+ return dfs ( 0 ) ;
26
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간 복잡도: O(n + m)
3
+ * 공간 복잡도: O(1)
4
+ */
5
+ var mergeTwoLists = function ( list1 , list2 ) {
6
+ const dummy = new ListNode ( ) ;
7
+ node = dummy ;
8
+
9
+ while ( list1 && list2 ) {
10
+ if ( list1 . val < list2 . val ) {
11
+ node . next = list1 ;
12
+ list1 = list1 . next ;
13
+ } else {
14
+ node . next = list2 ;
15
+ list2 = list2 . next ;
16
+ }
17
+ node = node . next ;
18
+ }
19
+
20
+ node . next = list1 || list2 ;
21
+
22
+ return dummy . next ;
23
+ } ;
You can’t perform that action at this time.
0 commit comments