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+ dfs๋ก ๊ฐ candidates๋ฅผ ๋๋ฉด์ target๊ณผ ๊ฐ์์ง๋ ์๊ฐ์ result๋ฅผ ๋ฐํํ๋ค.
3+
4+ ๊ฐ์ ์กฐํฉ์ด ์ค๋ณต๋จ์ ํผํ๊ธฐ ์ํด candidates๋ฅผ ์ ๋ ฌํ๊ณ , ๊ฐ ์ซ์๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ candidate๋ง ํ์ํ๋ค.
5+
6+ ์๊ฐ๋ณต์ก๋ : O(N^(T/min(c))) - N์ candidates์ ์, T๋ ํ๊ฒ, c๋ candidates.
7+ ํ๊ฒ์ candidates์ ์ต์๊ฐ์ผ๋ก ๋๋ ๊ฐ์ด ์ต๋ ๊น์ด์ด๋ฏ๋ก ์ต์
์ ๊ฒฝ์ฐ ํด๋น ํ์๋งํผ ๋ฐ๋ณตํด์ ๋ฐฐ์ด์ ํ์ํจ
8+ */
9+
10+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
11+ candidates . sort ( ( a , b ) => a - b )
12+ const result = [ ]
13+ const search = ( idx , nums , sum ) => {
14+ if ( sum === target ) {
15+ result . push ( nums )
16+ return
17+ }
18+ for ( let i = idx ; i < candidates . length ; i ++ ) {
19+ const num = candidates [ i ]
20+ if ( sum + num > target ) return
21+ search ( i , [ ...nums , num ] , sum + num )
22+ }
23+ }
24+
25+ search ( 0 , [ ] , 0 )
26+
27+ return result
28+ }
Original file line number Diff line number Diff line change 1+ /*
2+ dp ๋ฐฐ์ด์ ๋ง๋ค๊ณ s๋ฅผ ์ํํ๋ฉฐ ๊ฐ ๋จ๊ณ์์ ์ด์ ์๋ฆฌ์ ์ซ์๋ฅผ ์ฑํํ๊ฑฐ๋ ๋ฒ๋ฆฌ๋ ๊ฒฝ์ฐ๋ฅผ ๋ํด ๋๊ฐ๋ค
3+ ๋ง์ฝ ํ์ฌ ์๋ฒ์ ์ซ์๊ฐ 0์ผ ๊ฒฝ์ฐ : ์ด์ ์๋ฆฌ์ ์ซ์๋ฅผ ์ฑํ, i-1 ์๋ฒ์ ์ซ์๊ฐ 0์ด๊ฑฐ๋ 3 ์ด์์ด๋ฉด ์ฆ์ 0์ ๋ฆฌํด. ์๋ ๊ฒฝ์ฐ dp์ i-2๋ฒ์งธ๊ฐ์ ๋ฃ๋๋ค.
4+ ์ด์ ์๋ฒ์ ์ซ์์ ํ์ฌ ์ซ์๋ฅผ ๋ํ ๊ฐ์ด 26๋ณด๋ค ํฌ๊ฑฐ๋ ์ด์ ์๋ฒ์ ์ซ์๊ฐ 0์ผ ๊ฒฝ์ฐ : ๊ฐ๋ณ ์๋ฆฌ์ ์ซ์๋ฅผ ์ฑํ, ์ง์ dp๊ฐ์ ๋ฃ๋๋ค
5+ ์์ ๋ชจ๋ ํด๋นํ์ง ์์ ๊ฒฝ์ฐ : ๊ฐ๋ณ ์๋ฆฌ์ ์ซ์๋ฅผ ์ฑํํ๊ฑฐ๋ ๋ฒ๋ฆฌ๋ ๊ฒฝ์ฐ๋ฅผ ๋ค ๊ณ ๋ คํ์ฌ ์ง์ dp๊ฐ๊ณผ 2์คํ
์ dp๊ฐ์ ๋ํ๋ค
6+ ๋งจ ๋ ์๋ฒ์ ๋๋ฌํ์ ๋์ ๊ฐ์ ๊ตฌํ๋ค
7+
8+ ์๊ฐ๋ณต์ก๋ : O(N) (N์ s์ ๊ธธ์ด)
9+ */
10+
11+ function numDecodings ( s : string ) : number {
12+ if ( s . startsWith ( "0" ) ) return 0 ;
13+ const dp = [ ] ;
14+
15+ dp . push ( 1 ) ;
16+ if ( s . length < 2 ) return dp [ 0 ]
17+ for ( let i = 1 ; i < s . length ; i ++ ) {
18+ const numStr = s [ i ] ;
19+ if ( numStr === "0" ) {
20+ if ( s [ i - 1 ] === "0" || Number ( s [ i - 1 ] ) >= 3 ) return 0 ;
21+ dp . push ( dp ?. [ i - 2 ] ?? 1 )
22+ } else if ( Number ( `${ s [ i - 1 ] } ${ numStr } ` ) > 26 || s [ i - 1 ] === "0" ) {
23+ dp . push ( dp [ i - 1 ] )
24+ } else {
25+ dp . push ( dp [ i - 1 ] + ( dp ?. [ i - 2 ] ?? 1 ) )
26+ }
27+ }
28+
29+ return dp [ s . length - 1 ] ;
30+ } ;
Original file line number Diff line number Diff line change 1+ /*
2+ nums๋ฅผ ์ํํ๋ฉฐ ์ต๋ sum์ ์ฐพ๋๋ค. ๋ค์ ๊ท์น์ ๋ฐ๋ฅธ๋ค
3+ - ๋งค ์๊ฐ ์ซ์๋ฅผ ๋ํด ๊ฐ๋ฉฐ ํ์ฌ๊น์ง์ ํฉ๊ณผ ์ต๋ํฉ ๋น๊ต, ๊ฐฑ์
4+ - ๋ง์ฝ ํด๋น ์ธ๋ฑ์ค num์ ๋ํ์ ๋ 0๋ณด๋ค ์์์ง๋ฉด ํ์ฌ๊น์ง์ ๊ฐ์ 0์ผ๋ก ์ด๊ธฐํ(์ดํฉ ๋ฒ๋ฆผ)
5+
6+ */
7+
8+ function maxSubArray ( nums : number [ ] ) : number {
9+ let result = - Infinity ;
10+ let curSum = 0 ;
11+
12+ for ( let num of nums ) {
13+ curSum += num ;
14+ result = Math . max ( result , curSum ) ;
15+ if ( curSum < 0 ) curSum = 0 ;
16+ }
17+
18+ return result ;
19+ }
Original file line number Diff line number Diff line change 1+ /**
2+ n์ ์ด์ง์ ๋ณํํ๊ณ 1์ ๊ฐ์๋ฅผ ์ผ๋ค
3+
4+ ์๊ฐ๋ณต์ก๋ O(N) (N์ ์ซ์ n์ ๋นํธ ๊ฐ์)
5+ */
6+
7+ function hammingWeight ( n : number ) : number {
8+ const binaryNum = n . toString ( 2 )
9+ let result = 0
10+ for ( let char of binaryNum ) {
11+ if ( char === '1' ) result += 1
12+ }
13+ return result
14+ }
Original file line number Diff line number Diff line change 1+ /*
2+ s์์ ์ํ๋ฒณ๊ณผ ์ซ์๊ฐ ์๋ ๊ธ์๋ฅผ ๋ค ๋ ๋ฆฌ๊ณ , ์คํ์ ๋ฌธ์๋ฅผ ์ ๋ฐ๊น์ง ๋ด์ ํ ์ ๋ฐ ์ดํ๋ถํฐ๋ ๊บผ๋ด๊ฐ๋ฉฐ ์๋ค ๋ ํฐ๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ค
3+
4+ ์๊ฐ๋ณต์ก๋ : O(N) (N์ s์ ๊ธธ์ด)
5+ ๊ณต๊ฐ๋ณต์ก๋ : O(N) (์คํ)
6+ */
7+
8+ function isPalindrome ( s : string ) : boolean {
9+ const newStr = s . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) . toLowerCase ( )
10+ const isStrEven = newStr . length % 2 === 0
11+ const lenHalf = Math . floor ( newStr . length / 2 )
12+ const stack = [ ]
13+ for ( let i = 0 ; i < newStr . length ; i ++ ) {
14+ if ( i < lenHalf ) {
15+ stack . push ( newStr [ i ] )
16+ } else {
17+ if ( ! isStrEven && i === lenHalf ) continue
18+ const isSame = newStr [ i ] === stack . pop ( )
19+ if ( ! isSame ) return false
20+ }
21+ }
22+ return true
23+ }
You canโt perform that action at this time.
0 commit comments