[Python3] - Easy to Understand Prefix Product
This approach is similar to the one for prefix sum, where we keep a list of all the products up to index = i:\ne.g. If the given input of numbers is: [3, 4, 5],
This approach is similar to the one for prefix sum, where we keep a list of all the products up to index = i:\ne.g. If the given input of numbers is: ```[3, 4, 5]```, the prefix product list that we keep will be: ```[3, 3x4, 3x4x5] => [3, 12, 60]```\n\nEvery time we are fed with a zero, say at index i, we know that if the k is the range where it contains index i, the product will always be zero:\n\ne.g  Given input: ```[3, 0, 2, 5, 4]``` , when ```k >= 4```, the product is always 0.\n\nTherefore, whenever we see a zero, we could discard the product list we have kept so far.\n\n```\nclass ProductOfNumbers:\n\n    def __init__(self):\n        self.products = [] # containing the prefix producr\n\n    def add(self, num: int) -> None:\n        if num == 0:\n            self.products = [] # discard product list so far\n        else:\n            if self.products: \n                self.products.append(self.products[-1] * num) # if product list not empty, \n\t\t\t\t                                              # calculate the prefix product \n            else:\n                self.products.append(num)\n            \n    def getProduct(self, k: int) -> int:\n        if len(self.products) == k:\n            return self.products[-1]\n        elif len(self.products) > k:\n            return self.products[-1] // self.products[len(self.products) - k - 1]\n        else:\n            return 0\n```\n\n
['Python3']