ZhongKuo0228 / study

0 stars 0 forks source link

238. Product of Array Except Self #28

Open fockspaces opened 1 year ago

fockspaces commented 1 year ago

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 1:

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

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

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.

Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

fockspaces commented 1 year ago

pre[i] : nums[i]之前元素的products post[i] : nums[i] 之後元素的products 求出後再相乘,即除nums[i]外的products

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> pre(n, 1), post(n, 1);
        for(int i = 1; i < n; i++) 
            pre[i] = pre[i - 1] * nums[i - 1];
        for(int i = n - 2; i >= 0; i--) 
            post[i] = post[i + 1] * nums[i + 1];

        for(int i = 0; i < n; i++) pre[i] *= post[i];
        return pre;
    }
};

// dp1 : product before i
// dp2 : product after i from end
// 1, 2, 3, 4
// 1  1  2  6
//24 12  4  1
//24 12  8  6
fockspaces commented 1 year ago
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        n = len(nums)
        prev_prod, post_prod = [1] * len(nums), [1] * n
        for i in range(1, n):
            prev_prod[i] = prev_prod[i - 1] * nums[i - 1]

        for i in range(n -2, -1, -1):
            post_prod[i] = post_prod[i + 1] * nums[i + 1]

        return [post_prod[i] * prev_prod[i] for i in range(n)]
fockspaces commented 1 year ago

再更進一步將 post_prod 變為 O(1)

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        n, post_prod = len(nums), 1
        prev_prod = [1] * n
        for i in range(1, n):
            prev_prod[i] = prev_prod[i - 1] * nums[i - 1]

        for i in range(n - 1, -1, -1):
            prev_prod[i] *= post_prod
            post_prod *= nums[i]

        return prev_prod
fockspaces commented 9 months ago

simply maintan the prodcuts from previous and products from behind. and the product of the prev, suf pair is the answer

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        n = len(nums)
        pre_prods = [1] * n
        suf_prods = [1] * n
        for i in range(1, n):
            pre_prods[i] = pre_prods[i - 1] * nums[i - 1]
        for i in range(n - 2, -1, -1):
            suf_prods[i] = suf_prods[i + 1] * nums[i + 1]
        return [pre * suf for pre, suf in zip(pre_prods, suf_prods)]
fockspaces commented 9 months ago

we can eliminate the suf_prods to make it as int type

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        n = len(nums)
        pre_prods = [1] * n
        suf_prod = nums[-1]
        for i in range(1, n):
            pre_prods[i] = pre_prods[i - 1] * nums[i - 1]
        for i in range(n - 2, -1, -1):
            pre_prods[i] *= suf_prod
            suf_prod *= nums[i]
        return pre_prods