Skip to content

Commit 4fa6523

Browse files
committed
Add product-of-array-except-self solution
1 parent 40d6d56 commit 4fa6523

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
6+
// Time Complexity: O(n)
7+
// Space Complexity: O(n)
8+
var productExceptSelf = function (nums) {
9+
10+
let result = [];
11+
12+
let left = Array(nums.length).fill(1)
13+
let right = Array(nums.length).fill(1)
14+
15+
for (let i = 1; i < nums.length; i++) {
16+
17+
left[i] = left[i - 1] * nums[i - 1];
18+
right[nums.length - 1 - i] = right[right.length - i] * nums[nums.length - i]
19+
20+
}
21+
22+
for (let i = 0; i < nums.length; i++) {
23+
result[i] = left[i] * right[i];
24+
}
25+
26+
return result;
27+
28+
29+
};
30+

0 commit comments

Comments
 (0)