Skip to content

Commit ac12f16

Browse files
committed
product-of-array-except-self solution
1 parent cb3dbc0 commit ac12f16

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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);

0 commit comments

Comments
ย (0)