Skip to content

Commit d7a89af

Browse files
Problem 238 (#35)
* add initial solution * add optimal solution * update readme
1 parent f876d15 commit d7a89af

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
4545
34. [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
4646
35. [Implement Trie (Prefix Tree) #208](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/implement-trie-208.md)
4747
36. [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
48-
37. Product of Array Except Self #238
48+
37. [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
4949
38. Min Stack #155
5050
39. Validate Binary Search Tree #98
5151
40. Number of Islands #200
@@ -110,6 +110,7 @@ In order to practice with similar data structures I'll be placing each problem i
110110
- [Maximum Units on a Truck #1710](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/max-units-truck-1710.md)
111111
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
112112
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
113+
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
113114

114115
### Queue
115116

medium/product-of-array-238.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Product of Array Except Self
2+
3+
Page on leetcode: https://leetcode.com/problems/product-of-array-except-self/
4+
5+
## Problem Statement
6+
7+
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].
8+
9+
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
10+
11+
You must write an algorithm that runs in O(n) time and without using the division operation.
12+
13+
### Constraints
14+
15+
- 2 <= nums.length <= 105
16+
- -30 <= nums[i] <= 30
17+
- The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
18+
19+
### Example
20+
21+
```
22+
Input: nums = [1,2,3,4]
23+
Output: [24,12,8,6]
24+
```
25+
26+
```
27+
Input: nums = [-1,1,0,-3,3]
28+
Output: [0,0,9,0,0]
29+
```
30+
31+
## Solution
32+
33+
- Calculate the total product and store in variable
34+
- Iterate thru array a second time and subtrack product of current element
35+
- Can I store product left and right?
36+
- Will flipping or doing something in reverse work?
37+
38+
### Initial Solution
39+
40+
This solution has a time and space complexity of O(n).
41+
42+
```javascript
43+
const productExceptSelf = function (nums) {
44+
// Calculate left and right products
45+
const left = [];
46+
for (let i = 0; i < nums.length; i++) {
47+
if (i > 0) {
48+
const product = nums[i - 1] * left[left.length - 1];
49+
left.push(product);
50+
} else {
51+
left.push(1);
52+
}
53+
}
54+
const right = [];
55+
for (let i = nums.length - 1; i >= 0; i--) {
56+
if (i < nums.length - 1) {
57+
const product = nums[i + 1] * right[0];
58+
right.unshift(product);
59+
} else {
60+
right.unshift(1);
61+
}
62+
}
63+
console.log(left, right);
64+
// Calculate result based on what is left and right of current number
65+
const answer = [];
66+
for (let i = 0; i < nums.length; i++) {
67+
answer.push(left[i] * right[i]);
68+
}
69+
return answer;
70+
};
71+
```
72+
73+
### Optimized Solution
74+
75+
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
76+
77+
```javascript
78+
const productExceptSelf = function (nums) {
79+
// Create initial array and put initial value of 1
80+
const output = Array(nums.length).fill(1);
81+
82+
// Iterate thru nums and add prefixes to output
83+
let prefix = 1;
84+
for (let i = 0; i < nums.length; i++) {
85+
output[i] = prefix;
86+
prefix *= nums[i];
87+
}
88+
89+
// Iterate thru nums backwards and calculate total with pre and post fix.
90+
let postfix = 1;
91+
for (let i = nums.length - 1; i >= 0; i--) {
92+
output[i] *= postfix;
93+
postfix *= nums[i];
94+
}
95+
96+
return output;
97+
};
98+
```

0 commit comments

Comments
 (0)