diff --git a/238-product-of-array-except-self/product-of-array-except-self.py b/238-product-of-array-except-self/product-of-array-except-self.py new file mode 100644 index 0000000..91f41a3 --- /dev/null +++ b/238-product-of-array-except-self/product-of-array-except-self.py @@ -0,0 +1,16 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + + prefix = [1] * n + suffix = [1] * n + + for i in range(1, n): + prefix[i] = prefix[i - 1] * nums[i - 1] + + for i in range(n - 2, -1, -1): + suffix[i] = suffix[i + 1] * nums[i + 1] + + answer = [prefix[i] * suffix[i] for i in range(n)] + + return answer \ No newline at end of file