-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductExceptSelf.js
51 lines (44 loc) · 938 Bytes
/
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
/**
* @param {number[]} nums
* @return {number[]}
*/
//Array.reduce makes it O(n^2) time complexity
var productExceptSelf = function(nums) {
let ans = [];
if (nums.length == 0) return ans;
for(i = 0; i<nums.length; i++){
ans[i] = getProduct(nums, i);
}
return ans;
};
var getProduct = function(nums, i){
let changed = false;
if(!nums[i]){
nums[i] = 1;
changed = true;
}
let result = nums.reduce((acc, num) => acc * num, 1/nums[i])
if(changed){
nums[i] = 0;
}
return result
}
/**
* @param {number[]} nums
* @return {number[]}
*/
//O(n) time complexity solution
function productExceptSelf(nums) {
const answer = [];
let prefix = 1;
let postfix = 1;
for (let i = 0; i < nums.length; i++) {
answer[i] = prefix;
prefix *= nums[i];
}
for (let i = nums.length - 2; i >= 0; i--) {
postfix *= nums[i + 1];
answer[i] *= postfix;
}
return answer;
};