Skip to content

Commit f84dc87

Browse files
committed
feat: 238. Product of Array Except Self
1 parent 7e66c54 commit f84dc87

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// T.C: O(n)
2+
// S.C: O(n)
3+
function productExceptSelf(nums: number[]): number[] {
4+
const arr: number[] = new Array(nums.length);
5+
let product = 1;
6+
for (let i = 0; i < nums.length; i++) {
7+
arr[i] = product;
8+
product *= nums[i];
9+
}
10+
11+
product = 1;
12+
for (let i = nums.length - 1; i >= 0; i--) {
13+
arr[i] *= product;
14+
product *= nums[i];
15+
}
16+
17+
return arr;
18+
};

0 commit comments

Comments
 (0)