Product of Array Except Self

Medium attempt 1(exceed time limit)

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        dic  = {}
        for i in nums:
            temp = nums[:]
            temp.remove(i)
            dic[i] = temp
        print(dic)
        output=[]
        for i in nums:
            start=1
            for j in dic[i]:
                start = start * j
            output.append(start)
        return output

accpeted

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        output = [1]  * (len(nums))
        prefix = 1
        for i in range(len(nums)):
            output[i] = prefix
            prefix *= nums[i]
        postfix = 1
        for i in range(len(nums) - 1, -1, -1):
            output[i] *= postfix
            postfix *= nums[i]
	    return output

This bascially means that we times every number before the current number and every number after the current number together.

Create a list name output and inilizae them with 1
create variable prefix = 1
for every index in nums
	output[index] = prefix
	prefix times itself with nums[index]
create variable postfix =1
for every index in nums backwards:
	output[index] times itself with postfix
	postfix times itself with nums[index]
return output list