File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * 시간복잡도 O(n^(T/m))
3+ * 풀이방법: DFS 백트래킹
4+ * 결과: 2ms
5+ * @param {number[] } candidates
6+ * @param {number } target
7+ * @return {number[][] }
8+ */
9+ const combinationSum = function ( candidates , target ) {
10+ const result = [ ] ;
11+ const nums = [ ] ;
12+
13+ const sortedCandidates = candidates . sort ( ( a , b ) => a - b ) ;
14+
15+ function dfs ( start , sum ) {
16+ if ( sum === target ) {
17+ result . push ( [ ...nums ] ) ;
18+ }
19+
20+ for ( let i = start ; i < sortedCandidates . length ; i += 1 ) {
21+ const num = sortedCandidates [ i ] ;
22+
23+ if ( sum + num > target ) break ;
24+
25+ nums . push ( num ) ;
26+ dfs ( i , sum + num ) ;
27+ nums . pop ( ) ;
28+ }
29+ }
30+
31+ dfs ( 0 , 0 ) ;
32+
33+ return result ;
34+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간복잡도: O(n)
3+ * 실행속도: 65ms
4+ * @param {string } s
5+ * @return {number }
6+ */
7+ const numDecodings = function ( s ) {
8+ const n = s . length ;
9+
10+ const memo = new Array ( n + 1 ) . fill ( 0 ) ;
11+
12+ function dfs ( i ) {
13+ if ( i === n ) {
14+ return 1 ;
15+ }
16+
17+ if ( s [ i ] === "0" ) {
18+ return 0 ;
19+ }
20+
21+ if ( memo [ i ] ) {
22+ return memo [ i ] ;
23+ }
24+
25+ let res = dfs ( i + 1 ) ;
26+
27+ if ( i + 1 < n && Number ( s . slice ( i , i + 2 ) <= 26 ) ) {
28+ res += dfs ( i + 2 ) ;
29+ }
30+
31+ memo [ i ] = res ;
32+
33+ return res ;
34+ }
35+
36+ return dfs [ 0 ] ;
37+ } ;
38+
39+ /**
40+ * 시간복잡도: O(n)
41+ * 실행속도: 0~1ms
42+ * @param {string } s
43+ * @return {number }
44+ */
45+ /**
46+ * 시간 복잡도: O(n)
47+ * 공간 복잡도: O(n)
48+ */
49+ var numDecodings2 = function ( s ) {
50+ const memo = new Map ( ) ;
51+ memo . set ( s . length , 1 ) ;
52+
53+ function dfs ( start ) {
54+ if ( memo . has ( start ) ) {
55+ return memo . get ( start ) ;
56+ }
57+
58+ if ( s [ start ] === "0" ) {
59+ memo . set ( start , 0 ) ;
60+ } else if (
61+ start + 1 < s . length &&
62+ parseInt ( s . slice ( start , start + 2 ) ) < 27
63+ ) {
64+ memo . set ( start , dfs ( start + 1 ) + dfs ( start + 2 ) ) ;
65+ } else {
66+ memo . set ( start , dfs ( start + 1 ) ) ;
67+ }
68+
69+ return memo . get ( start ) ;
70+ }
71+
72+ return dfs ( 0 ) ;
73+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간복잡도: O(n)
3+ * 시간: 4ms
4+ * @param {number[] } nums
5+ * @return {number }
6+ */
7+ var maxSubArray = function ( nums ) {
8+ let maxResult = nums [ 0 ] ;
9+ let curResult = nums [ 0 ] ;
10+
11+ for ( let i = 1 ; i < nums . length ; i += 1 ) {
12+ const num = nums [ i ] ;
13+ // 지금까지 누적합 vs 현재 날짜
14+ result = Math . max ( curResult + num , num ) ;
15+ maxResult = Math . max ( maxResult , curResult ) ;
16+ }
17+
18+ return maxResult ;
19+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간복잡도: O(n)
3+ * 공간복잡도: O(1)
4+ * @param {number } n
5+ * @return {number }
6+ */
7+ var hammingWeight = function ( n ) {
8+ let num = n ;
9+
10+ let count = 0 ;
11+
12+ while ( num > 0 ) {
13+ const left = num % 2 ;
14+
15+ if ( left === 1 ) {
16+ count += 1 ;
17+ }
18+
19+ num = Math . floor ( num / 2 ) ;
20+ }
21+
22+ return count ;
23+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간복잡도: O(N)
3+ * 공간복잡도: O(1)
4+ * @param {string } s
5+ * @return {boolean }
6+ */
7+ const isPalindrome = function ( s ) {
8+ const parsedString = s
9+ . trim ( )
10+ . replace ( " " , "" )
11+ . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" )
12+ . toLowerCase ( ) ;
13+
14+ let left = 0 ;
15+ let right = parsedString . length - 1 ;
16+
17+ while ( left < right ) {
18+ if ( parsedString [ left ] !== parsedString [ right ] ) {
19+ return false ;
20+ }
21+
22+ left += 1 ;
23+ right -= 1 ;
24+ }
25+
26+ return true ;
27+ } ;
You can’t perform that action at this time.
0 commit comments