We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5e82ba0 commit a21cff6Copy full SHA for a21cff6
product-of-array-except-self/junzero741.ts
@@ -0,0 +1,19 @@
1
+// TC: O(n)
2
+// SC: O(1)
3
+function productExceptSelf(nums: number[]): number[] {
4
+ const answer = new Array(nums.length).fill(1);
5
+
6
+ let before = 1;
7
+ for(let i = 0; i < nums.length-1; i++) {
8
+ before *= nums[i];
9
+ answer[i+1] *= before;
10
+ }
11
12
+ before = 1;
13
+ for(let i = nums.length-1; i > 0; i--) {
14
15
+ answer[i-1] *= before;
16
17
18
+ return answer;
19
+};
0 commit comments