File tree Expand file tree Collapse file tree 3 files changed +99
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
3+ * - ์ค๋ณต ํฌํจํ์ฌ ๋ชจ๋ ์กฐํฉ ๊ตฌํด์ผ ํ๋๊น ์ฌ๊ทํจ์๋ก ํ๊ธฐ
4+ * - ์ฌ๊ท ํธ์ถ๋ก ํ์ํด์ผํ๋ ํ๊ฒ ์ค์ฌ๊ฐ๋ฉด์ ์กฐํฉ ๋ง๋ค๊ธฐ
5+ * - ๋์ผ ์กฐํฉ์ถ๊ฐ๋์ง ์๋๋ก, startIndex ์ถ๊ฐํ์ฌ ๋ค์ ์ธ๋ฑ์ค๋ถํฐ ์ํํ๋๋ก ์ ํ
6+ *
7+ *
8+ * ์๊ฐ๋ณต์ก๋ : O(n^target)
9+ * - candidates ๋ฐฐ์ด ๊ธธ์ด n๋งํผ ์ฌ๊ท๊ฐ ํธ์ถ๋๊ณ , ๊ฐ ํธ์ถ์ target ๊ธธ์ด ๋งํผ ์ค์ฒฉ๋๋๊น O(n^target)
10+ *
11+ * ๊ณต๊ฐ๋ณต์ก๋ : O(target)
12+ * - ์ต์
์ ๊ฒฝ์ฐ target๋งํผ ์ฌ๊ท ํธ์ถ๋๋๊น O(target)
13+ *
14+ */
15+
16+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
17+ const result : number [ ] [ ] = [ ] ;
18+
19+ const dfs = ( target : number , combination : number [ ] , startIndex : number ) => {
20+ if ( target === 0 ) {
21+ result . push ( [ ...combination ] ) ;
22+ return ;
23+ }
24+
25+ if ( target < 0 ) return ;
26+
27+ for ( let i = startIndex ; i < candidates . length ; i ++ ) {
28+ combination . push ( candidates [ i ] ) ;
29+ dfs ( target - candidates [ i ] , combination , i ) ;
30+ combination . pop ( ) ;
31+ }
32+ } ;
33+
34+ dfs ( target , [ ] , 0 ) ;
35+
36+ return result ;
37+ }
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4+ * - O(n)์ผ๋ก ํ์ด์ผ ํ๋๊น ์ค์ฒฉ์ด ์๋ ๋ฐฐ์ด ๊ฐ๋ณ๋ก 2๋ฒ ์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทผ
5+ * - ์ผ์ชฝ ๊ณฑ(prefixProduct)๊ณผ ์ค๋ฅธ์ชฝ ๊ณฑ((suffixProduct)์ ๋ฐ๋ก ๊ณ์ฐํด์ ๊ฒฐ๊ณผ๊ฐ์ ์ ์ฅ
6+ *
7+ * ์๊ฐ๋ณต์ก๋ : O(n)
8+ * - ๋ฐฐ์ด ๊ธธ์ด๋งํผ ์ํํ๋๊น O(n)
9+ *
10+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
11+ * - ๋ฐฐ์ด ๊ธธ์ด๋งํผ ๊ฒฐ๊ณผ๊ฐ ์ ์ฅํ๋๊น O(n)
12+ *
13+ */
14+
15+ function productExceptSelf ( nums : number [ ] ) : number [ ] {
16+ let result : number [ ] = Array ( nums . length ) . fill ( 1 ) ;
17+ let prefixProduct = 1 ;
18+ let suffixProduct = 1 ;
19+
20+ for ( let i = 0 ; i < nums . length ; i ++ ) {
21+ result [ i ] = prefixProduct ;
22+ prefixProduct *= nums [ i ] ;
23+ }
24+
25+ for ( let i = nums . length - 1 ; i >= 0 ; i -- ) {
26+ result [ i ] *= suffixProduct ;
27+ suffixProduct *= nums [ i ] ;
28+ }
29+
30+ return result ;
31+ }
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4+ * - ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ๋งต์ ์ ์ฅ
5+ * - nums ๋ฐฐ์ด ์ํํ๋ฉด์, ์ฐพ๋ ์ซ์ ๊ฐ ์์ผ๋ฉด ๋งต์ ๊ฐ, ์ธ๋ฑ์ค ์ถ๊ฐํ๊ธฐ
6+ * - ๋งต์ ์กด์ฌํ๋ฉด ํ์ฌ ์ธ๋ฑ์ค์, ํด๋น ์ซ์์ ์ธ๋ฑ์ค ๋ด์์ ์ฆ์ ๋ฆฌํดํ๊ธฐ
7+ *
8+ * ์๊ฐ๋ณต์ก๋ : O(n)
9+ * - nums ๋ฐฐ์ด ๊ธธ์ด๋งํผ 1ํ ์ํํ๋๊น O(n)
10+ * - ๋งต ์กฐํ ๋ฐ ์ฝ์
์ O(1)
11+ *
12+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
13+ * - ๋งต์ ๋ฐฐ์ด์ ๊ฐ ์ ์ฅํ๋๊น O(n)
14+ *
15+ * ์ฃ์ง ์ผ์ด์ค :
16+ * - ๋์ผํ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ : [3, 3], 6 => [0,1]
17+ */
18+
19+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
20+ const map = new Map < number , number > ( ) ;
21+
22+ for ( let i = 0 ; i < nums . length ; i ++ ) {
23+ const neededValue = target - nums [ i ] ;
24+
25+ if ( map . has ( neededValue ) ) {
26+ return [ map . get ( neededValue ) ! , i ] ;
27+ }
28+
29+ map . set ( nums [ i ] , i ) ;
30+ }
31+ }
You canโt perform that action at this time.
0 commit comments