We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3a10516 commit fddee5cCopy full SHA for fddee5c
product-of-array-except-self/sunjae95.js
@@ -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