Skip to content

Commit fddee5c

Browse files
committed
Product Of Array Except Self
1 parent 3a10516 commit fddee5c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* recursive function
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
var productExceptSelf = function (nums) {
10+
const answer = Array.from({ length: nums.length }, () => 0);
11+
12+
const search = (left, right, i) => {
13+
if (i === nums.length - 1) {
14+
answer[i] = left * right;
15+
return nums[i];
16+
}
17+
18+
const productLeft = left * nums[i];
19+
const productRight = search(productLeft, right, i + 1);
20+
21+
answer[i] = left * productRight;
22+
23+
return productRight * nums[i];
24+
};
25+
26+
search(1, 1, 0);
27+
28+
return answer;
29+
};

0 commit comments

Comments
 (0)