-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-of-array-except-self.ts
64 lines (50 loc) · 1.55 KB
/
product-of-array-except-self.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Given an integer array nums, return an array answer such that answer[i] is
equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Constraints:
2 <= nums.length <= 105
-30 <= nums[i] <= 30
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
Follow up: Can you solve the problem in O(1) extra space complexity?
(The output array does not count as extra space for space complexity analysis.)
*/
const EXAMPLES = [
{
input: [1, 2, 3, 4],
output: [24, 12, 8, 6],
},
{
input: [-1, 1, 0, -3, 3],
output: [0, 0, 9, 0, 0],
},
];
/** Time O(n) | Space O(1) */
function getProductOfArrayExceptSelf(nums: number[]): number[] {
const result = new Array(nums.length).fill(1);
let prefix = 1;
for (let i = 0; i < nums.length; i++) {
result[i] *= prefix;
prefix *= nums[i];
}
let postfix = 1;
for (let i = nums.length - 1; i >= 0; i--) {
result[i] *= postfix;
postfix *= nums[i];
}
return result;
}
function removeNegativeZeros(nums: number[]): number[] {
return nums.map((num) => (Object.is(-0, num) ? 0 : num));
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('Product of Array Except Self', () => {
EXAMPLES.forEach((example) => {
expect(
removeNegativeZeros(getProductOfArrayExceptSelf(example.input)),
).toEqual(example.output);
});
});
}