238. Product of Array Except Self

class Solution(object):
	def productExceptSelf(self, nums):
	
	res = [1] * len(nums)
	
	prefix = 1
	for i in range(len(nums)):
		res[i] *= prefix
		prefix *= nums[i]
	
	postfix = 1
	
	# trick to iterate backwards
	for i in range(len(nums) - 1, -1, -1):
		res[i] *= postfix
		postfix *= nums[i]
	

	return res

CleanShot 2024-03-02 at 17.15.06.png