238. Product of Array Except Self

202401162309
tags: #prefix-sum

class Solution(object):
	def productExceptSelf(self, nums):
		res = len(nums) * [1]
		leftProduct = 1
		for i in range(len(nums)):
			res[i] *= leftProduct
			leftProduct *= nums[i]
		rightProduct = 1
		for i in reversed(range(len(nums))):
			res[i] *= rightProduct
			rightProduct *= nums[i]
		return res

Reference

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