-
Notifications
You must be signed in to change notification settings - Fork 0
/
productExceptSelf.js
59 lines (51 loc) · 1.4 KB
/
productExceptSelf.js
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
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const prefix = Array(nums.length + 1).fill(1);
const postfix = Array(nums.length + 1).fill(1);
const ans = Array(nums.length);
// Note this solution is with O(n) memory , and O(n) time
// Answer(i) = prefix(i - 1 ) * postfix(i + 1)
// First calculate prefix & postfix.
for (let i = 0, j = nums.length - 1; i < nums.length; i++, j--) {
if (prefix[i] * nums[i] === -0) prefix[i + 1] = 0;
else prefix[i + 1] = prefix[i] * nums[i];
if (postfix[j + 1] * nums[j] === -0) postfix[j] = 0;
else postfix[j] = postfix[j + 1] * nums[j];
}
for (let i = 0; i < nums.length; i++) {
ans[i] = prefix[i] * postfix[i + 1];
}
return ans;
};
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelfV2 = function (nums) {
let prefix = 1;
let postfix = 1;
const ans = Array(nums.length);
// Note this solution is with O(1) memory since the ans isn't counted, and O(n) time
for (let i = 0; i < nums.length; i++) {
ans[i] = prefix;
prefix *= nums[i];
if (prefix === -0) {
prefix = 0;
}
}
for (let i = nums.length - 1; i >= 0; i--) {
ans[i] = postfix * ans[i];
postfix *= nums[i];
if (postfix === -0) {
postfix = 0;
}
if (ans[i] === -0) {
ans[i] = 0;
}
}
return ans;
};
console.log(productExceptSelfV2([1, 2, 3, 4]));