LC0238 - Product of Array Except Self (MEDIUM)#

This is the solution for Leetcode Problem 238 - Product of Array Except Self.

 1"""Solution to Leetcode 238 - 238
 2
 3Link : https://leetcode.com/problems/product-of-array-except-self/
 4Level: medium
 5"""
 6from typing import List
 7
 8
 9class Solution:
10    def __init__(self, *args, **kwargs):
11        """Instantiates the solution object"""
12        self.args = args
13        self.kwargs = kwargs
14
15    def solve(self, *args, **kwargs):
16        """This implements the main solution"""
17        raise NotImplementedError("This solution is not yet implemented.")
18
19    def productExceptSelf(self, nums: List[int]) -> List[int]:
20        # first construct 2 arrays,
21        left = [1] * len(nums)
22        right = [1] * len(nums)
23        for i in range(1, len(nums)):
24            left[i] = left[i - 1] * nums[i - 1]
25            right[-i - 1] = right[-i] * nums[-i]
26        result = [1] * len(nums)
27        for i in range(len(nums)):
28            result[i] = left[i] * right[i]
29        return result