File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param nums - ์ ์ ๋ฐฐ์ด
3+ * @returns - nums[i]๋ฅผ ์ ์ธํ ์์๋ค์ ๊ณฑ์
๊ฐ ๋ฐฐ์ด
4+ * @description
5+ * - ๋๋๊ธฐ๋ฅผ ํตํ ํ์ด๋ ๋งํ
6+ * - ์ผ์ชฝ, ์ค๋ฅธ์ชฝ์ผ๋ก ์ํํ๋ฉฐ ๊ณฑ์ ๋์ ํ๊ธฐ
7+ * - ์ฒซ๋ฒ์งธ ๋ฐ๋ณต - ์ฒ์ ๊ฐ์ ์์ ์ ์ ์ธํ ๋์ ์ ์ ์ฒด์ด๋ฏ๋ก 1, ์ดํ ๋ถํฐ ๋์
8+ * - ๋๋ฒ์งธ ๋ฐ๋ณต - ๋ง์ง๋ง ๊ฐ ๋ํ ์์ ์ ์ ์ธํ ์ฒซ๋ฒ์งธ ๋ฐ๋ณต์ ๋์ , ์ดํ ๋ถํฐ ๋ฏธ๋ฆฌ ๋์ ํ ๊ฐ๊ณผ ์์ ์ ๊ณฑ
9+ *
10+ * @description
11+ * - ์๊ฐ ๋ณต์ก๋ O(n)
12+ * - ๊ณต๊ฐ ๋ณต์ก๋ O(n)
13+ * - ํด๋น ๋ฐฉ๋ฒ์ ๋ ์ฌ๋ฆฌ์ง ๋ชปํด AI์ ๋์์ ๋ฐ์๋๋ฐ ์ํ์ ์ฌ๊ณ ๊ฐ ๋ ํ์ํ๋ค.
14+ */
15+ function productExceptSelf ( nums : number [ ] ) : number [ ] {
16+ let prev = 1 ;
17+ let next = 1 ;
18+ const arr : number [ ] = [ ] ;
19+
20+ for ( let i = 0 ; i < nums . length ; i ++ ) {
21+ arr . push ( prev ) ;
22+ prev = prev * nums [ i ] ;
23+ }
24+
25+ for ( let j = nums . length - 1 ; j >= 0 ; j -- ) {
26+ arr [ j ] = arr [ j ] * next ;
27+ next = next * nums [ j ] ;
28+ }
29+
30+ return arr ;
31+ }
32+
33+ const nums = [ 1 , 2 , 3 , 4 ] ;
34+ productExceptSelf ( nums ) ;
You canโt perform that action at this time.
0 commit comments