[Java/C++/Python] Prefix Product
Intuition\nSimilar to prefix sum. We can record the prefix product.\n\n\n# Explanation\nIf we meet 0, the product including this 0 will always be 0.\nWe only ne
# **Intuition**\nSimilar to prefix sum. We can record the prefix product.\n<br>\n\n# **Explanation**\nIf we meet 0, the product including this 0 will always be 0.\nWe only need to record the prefix product after it.\nSo I clear the `A` and reinitilise it as `[1]`,\nwhere 1 is the neutral element of multiplication.\n<br>\n\n# **Complexity**\nTime `O(1)` each\nSpace `O(N)`\n<br>\n\n**Java:**\n```java\n    ArrayList<Integer> A = new ArrayList(){{\n        add(1);\n    }};\n\n    public void add(int a) {\n        if (a > 0)\n            A.add(A.get(A.size() - 1) * a);\n        else {\n            A = new ArrayList();\n            A.add(1);\n        }\n    }\n\n    public int getProduct(int k) {\n        int n = A.size();\n        return k < n ? A.get(n - 1) / A.get(n - k - 1)  : 0;\n    }\n```\n\n**C++:**\n```cpp\n    vector<int> A = {1};\n    void add(int a) {\n        if (a)\n            A.push_back(A.back() * a);\n        else\n            A = {1};\n    }\n\n    int getProduct(int k) {\n        return k < A.size() ? A.back() / A[A.size() - k - 1]  : 0;\n    }\n```\n\n**Python:**\n```python\n    def __init__(self):\n        self.A = [1]\n\n    def add(self, a):\n        if a == 0:\n            self.A = [1]\n        else:\n            self.A.append(self.A[-1] * a)\n\n    def getProduct(self, k):\n        if k >= len(self.A): return 0\n        return self.A[-1] / self.A[-k - 1]\n```
[]