Product of Array Except Self Python

PHOTO EMBED

Tue Sep 20 2022 00:26:15 GMT+0000 (Coordinated Universal Time)

Saved by @bryantirawan #python #neetcode #sort

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        #if there were no time complexity restriction, you can use nested for loops and just continue if index of first for loop == index of second for loop 
        #if there were no divsion restriction, you can just find prod(nums) len(nums) times and divide by num[i] and append to result 
        
        res = [1] * len(nums) 
        
        prefix = 1 
        
        for i in range(len(nums)): 
            res[i] = prefix 
            prefix *= nums[i] 
        postfix = 1 
        for i in range(len(nums) - 1, -1, -1): 
            res[i] *= postfix 
            postfix *= nums[i] 
        return res 
        
        
        
            
#must be in O(N) time, O(1) space, and not use divide operator             
            
content_copyCOPY

Concept: find the product before the index and find the product after the index. Multiply together and replace in result array. https://ibb.co/6BSymJc

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