Python | Simple Prefix Product | O(1) / O(1), O(n) | Beats 96%
CodeComplexity

Time complexity: add=O(1), getProduct=O(1). append(), list[index], and list = [1] are all constant.


Space complexity: O(n). If no num = 0 then
# Code
```python3 []
class ProductOfNumbers:
    def __init__(self):
        self.pref_prod = [1] # stores product of all previous nums

    def add(self, num: int):
        if num == 0:
            self.pref_prod = [1] # reset, all previous prods are 0
        else:
            self.pref_prod.append(self.pref_prod[-1] * num) # add new product

    def getProduct(self, k: int) -> int:
        if k >= len(self.pref_prod): 
            return 0 # pref_prod doesn't include 0, [k:] would be 0 if wasn't removed

        return self.pref_prod[-1] // self.pref_prod[-k - 1] # total product / prod before k = prod of k
```

# Complexity
- Time complexity: $$add = O(1),\  getProduct = O(1)$$. append(), list[index], and list = [1] are all constant.

- Space complexity: $$O(n)$$. If no num = 0 then pref_prod holds all nums.

# Performance

![Screenshot (876).png](https://assets.leetcode.com/users/images/fdfcbb6e-5d92-45c1-94ca-c7c11e0110c1_1739493649.2707677.png)


['Array', 'Python3']