Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1579 from benmarg/productExceptSelf
Browse files Browse the repository at this point in the history
Update: 238-Product-Of-Array-Except-Self.js
  • Loading branch information
benmarg authored Dec 26, 2022
2 parents 7144043 + f51fb88 commit 97219d8
Showing 1 changed file with 14 additions and 62 deletions.
76 changes: 14 additions & 62 deletions javascript/238-Product-of-Array-Except-Self.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,19 @@
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = (nums) => {
const products = 2;
const [ leftProduct, rightProduct ] = getProducts(products, nums);

carryForward(nums, leftProduct); /* Time O(N) | Space O(N) */
carryBackward(nums, rightProduct); /* Time O(N) | Space O(N) */

return combineProducts(nums, leftProduct, rightProduct); /* Time O(N) | Ignore Auxillary Space O(N) */
}

const getProducts = (products, nums) => new Array(products).fill()
.map(() => new Array(nums.length).fill(1));

var carryForward = (nums, leftProduct) => {
for (let index = 1; index < nums.length; index++) {/* Time O(N) */
leftProduct[index] = nums[index - 1] * leftProduct[index - 1];/* Space O(N) */
function productExceptSelf(nums) {
const result = [];
let prefix = 1;
let postfix = 1;

for (let i = 0; i < nums.length; i++) {
result[i] = prefix;
prefix *= nums[i];
}
}

var carryBackward = (nums, rightProduct) => {
for (let index = (nums.length - 2); 0 <= index; index--) {/* Time O(N) */
rightProduct[index] = nums[index + 1] * rightProduct[index + 1];/* Space O(N) */
for (let i = nums.length - 2; i >= 0; i--) {
postfix *= nums[i + 1];
result[i] *= postfix;
}
}

const combineProducts = (nums, leftProduct, rightProduct) => {
const products = new Array(nums.length).fill(1);/* Ignore Auxillary Space O(N) */

for (let index = 0; index < nums.length; index++) {/* Time O(N) */
products[index] = leftProduct[index] * rightProduct[index];/* Ignore Auxillary Space O(N) */
}

return products;
}

/**
* Array - Ignore Auxillary Space
* Time O(N) | Space O(1)
* https://leetcode.com/problems/product-of-array-except-self/
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = (nums) => {
const products = new Array(nums.length).fill(1);/* Ignore Auxillary Space O(N) */

carryForward(nums, products); /* Time O(N) | Ignore Auxillary Space O(N) */
carryBackward(nums, products);/* Time O(N) | Ignore Auxillary Space O(N) */

return products;
};

var carryForward = (nums, products, product = 1) => {
for (let index = 0; index < nums.length; index++) {/* Time O(N) */
products[index] = product; /* Ignore Auxillary Space O(N) */
product *= nums[index];
}
}

var carryBackward = (nums, products, product = 1) => {
for (let index = (nums.length - 1); 0 <= index; index--) {/* Time O(N) */
products[index] *= product; /* Ignore Auxillary Space O(N) */
product *= nums[index];
}
}

return result;
};

0 comments on commit 97219d8

Please sign in to comment.