Skip to content

Problem 238 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
34. [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
35. [Implement Trie (Prefix Tree) #208](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/implement-trie-208.md)
36. [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
37. Product of Array Except Self #238
37. [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
38. Min Stack #155
39. Validate Binary Search Tree #98
40. Number of Islands #200
Expand Down Expand Up @@ -110,6 +110,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Maximum Units on a Truck #1710](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/max-units-truck-1710.md)
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)

### Queue

Expand Down
98 changes: 98 additions & 0 deletions medium/product-of-array-238.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Product of Array Except Self

Page on leetcode: https://leetcode.com/problems/product-of-array-except-self/

## Problem Statement

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.

### Example

```
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
```

```
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
```

## Solution

- Calculate the total product and store in variable
- Iterate thru array a second time and subtrack product of current element
- Can I store product left and right?
- Will flipping or doing something in reverse work?

### Initial Solution

This solution has a time and space complexity of O(n).

```javascript
const productExceptSelf = function (nums) {
// Calculate left and right products
const left = [];
for (let i = 0; i < nums.length; i++) {
if (i > 0) {
const product = nums[i - 1] * left[left.length - 1];
left.push(product);
} else {
left.push(1);
}
}
const right = [];
for (let i = nums.length - 1; i >= 0; i--) {
if (i < nums.length - 1) {
const product = nums[i + 1] * right[0];
right.unshift(product);
} else {
right.unshift(1);
}
}
console.log(left, right);
// Calculate result based on what is left and right of current number
const answer = [];
for (let i = 0; i < nums.length; i++) {
answer.push(left[i] * right[i]);
}
return answer;
};
```

### Optimized Solution

This solution has O(n) time complexity and O(1) space complexity. You can see an explanation of this solution here: https://www.youtube.com/watch?v=bNvIQI2wAjk

```javascript
const productExceptSelf = function (nums) {
// Create initial array and put initial value of 1
const output = Array(nums.length).fill(1);

// Iterate thru nums and add prefixes to output
let prefix = 1;
for (let i = 0; i < nums.length; i++) {
output[i] = prefix;
prefix *= nums[i];
}

// Iterate thru nums backwards and calculate total with pre and post fix.
let postfix = 1;
for (let i = nums.length - 1; i >= 0; i--) {
output[i] *= postfix;
postfix *= nums[i];
}

return output;
};
```