Skip to content
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

Product of Array Except Self #41

Open
shurintou opened this issue Mar 23, 2023 · 0 comments
Open

Product of Array Except Self #41

shurintou opened this issue Mar 23, 2023 · 0 comments
Labels
category:leetcode to note some leetcode question language:English blog written by Engish tag:javascript something about javascript

Comments

@shurintou
Copy link
Owner

shurintou commented Mar 23, 2023

Question

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.

Example

The question is not that difficult if there are no limit conditions, but since it need to be run in a O(n) time and and without using the division operation. I tried but didn't get a good answer.
And here is a good example I found. (It even has an O(1) Space Complexity!!)

/**
 * @param {number[]} nums
 * @return {number[]}
 */

var productExceptSelf = function (nums) {
    const resultArr = []
    for (let i = 0; i < nums.length; i++) {
        let accumulationProductVal = nums[i]
        if (resultArr.length !== 0)
            // This value is accumulated from start.
            accumulationProductVal = accumulationProductVal * resultArr[i - 1]
        resultArr.push(accumulationProductVal)
    }

    let accumulationProductVal = 1
    let i = nums.length - 1
    for (i; i > 0; i--) {
        resultArr[i] = resultArr[i - 1] * accumulationProductVal
        // This value is accumulated from end.
        accumulationProductVal = nums[i] * accumulationProductVal
    }
    resultArr[i] = accumulationProductVal

    return resultArr
}

You may not understand what this approach is doing just like I did first, but if you try to trace the operation you will understand it and find it is a great way to solve the problem.

The mechanism is very simple, which is that the each answer[i] is an product value accumulated start from the two side(the first and the last num) of the array, to the two num next to the num[i] itself.

@shurintou shurintou added language:English blog written by Engish tag:javascript something about javascript category:leetcode to note some leetcode question labels Jul 15, 2023
@shurintou shurintou changed the title new issue Product of Array Except Self Jul 15, 2023
@shurintou shurintou reopened this Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:leetcode to note some leetcode question language:English blog written by Engish tag:javascript something about javascript
Projects
None yet
Development

No branches or pull requests

1 participant