Skip to content

Commit 2f24f39

Browse files
committed
solve: product-of-array-except-self 문제 풀이 (실패)
1 parent 0f6f240 commit 2f24f39

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description
3+
* time complexity: O(n)
4+
* space complexity: O(n)
5+
* runtime: 5ms
6+
* 풀이 방법 : 40분고민하다가 다른사람 풀이보고 누적합을 이용하면 된다해서 구현함
7+
* @param {number[]} nums
8+
* @return {number[]}
9+
*/
10+
var productExceptSelf = function (nums) {
11+
const result = new Array(nums.length).fill(1);
12+
13+
let prefix = 1;
14+
15+
for (let i = 0; i < nums.length; i += 1) {
16+
result[i] = prefix;
17+
prefix *= nums[i];
18+
}
19+
20+
let postfix = 1;
21+
22+
for (let i = nums.length - 1; i >= 0; i -= 1) {
23+
result[i] *= postfix;
24+
postfix *= nums[i];
25+
}
26+
27+
return result;
28+
};

0 commit comments

Comments
 (0)