← Home

238. Product of Array Except Self

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
238

The trick here is to name the state correctly, then let the implementation follow. For 238. Product of Array Except Self, the solution in this repository is mainly a two pointers solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

The notes already sitting in the source point us in the right direction:

  • WA

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are productExceptSelf, output, L.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N), space O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//WA
02class Solution {
03public:
04    vector<int> productExceptSelf(vector<int>& nums) {
05        int product = accumulate(nums.begin(), nums.end(), 1, multiplies<int>());
06        int N = nums.size();
07        vector<int> output(N, 0);
08        
09        for(int i = 0; i < N; i++){
10            output[i] = product/nums[i];
11        }
12        
13        return output;
14    }
15};
16
17//Approach 1: Left and Right product lists
18//Runtime: 44 ms, faster than 31.25% of C++ online submissions for Product of Array Except Self.
19//Memory Usage: 10.7 MB, less than 100.00% of C++ online submissions for Product of Array Except Self.
20//time: O(N), space: O(N)
21class Solution {
22public:
23    vector<int> productExceptSelf(vector<int>& nums) {
24        int N = nums.size();
25        vector<int> L(N, 1), R(N, 1), output(N, 1);
26        
27        for(int i = 1; i < N; i++){
28            L[i] = L[i-1] * nums[i-1];
29        }
30        
31        for(int i = N-2; i >= 0; i--){
32            R[i] = R[i+1] * nums[i+1];
33        }
34        
35        for(int i = 0; i < N; i++){
36            output[i] = L[i] * R[i];
37        }
38        
39        return output;
40    }
41};
42
43//Optimize the array R
44//Runtime: 40 ms, faster than 75.01% of C++ online submissions for Product of Array Except Self.
45//Memory Usage: 10.4 MB, less than 100.00% of C++ online submissions for Product of Array Except Self.
46//time: O(N), space O(1)
47class Solution {
48public:
49    vector<int> productExceptSelf(vector<int>& nums) {
50        int N = nums.size();
51        int R = 1;
52        vector<int> ans(N, 1);
53        
54        for(int i = 1; i < N; i++){
55            ans[i] = ans[i-1] * nums[i-1];
56        }
57        
58        for(int i = N-2; i >= 0; i--){
59            R *= nums[i+1];
60            ans[i] *= R;
61        }
62        
63        return ans;
64    }
65};

Cost

Complexity

Time
O(N), space O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.