Straightforward solution || JAVA || Beats 100%
Intuition\nJust follow the hints ;)\n\nThe goal is to efficiently calculate the product of the last k numbers after each insertion. Instead of recalculating fro
# Intuition\nJust follow the hints ;)\n\nThe goal is to efficiently calculate the product of the last k numbers after each insertion. Instead of recalculating from scratch each time, we maintain a cumulative product list that allows us to access the desired product using division.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Maintain a list of cumulative products (products) where each element represents the product of all numbers added so far.\n2. For each number added:\n  - Multiply it with the last cumulative product and store the result.\n  - If the number is 0, reset the list because any product involving 0 will always be 0.\n3. When querying the product of the last k numbers, divide the current cumulative product by the product that represents the position just before the last k numbers.\n\n\n# Complexity\n\n- Time complexity:\nadd(num): O(1) per insertion.\ngetProduct(k): O(1) for retrieval by accessing the cumulative product list.\n\n- Space complexity:\nO(n), where n is the number of elements added (for storing cumulative products).\n\n# Code\n```java []\nclass ProductOfNumbers {\n\n    List<Integer> products;\n    int product;\n\n    public ProductOfNumbers() {\n        this.products = new ArrayList<Integer>();\n        this.product = 1;\n        products.add(product);\n    }\n    \n    public void add(int num) {\n        if(num<=0){\n            products = new ArrayList();\n            product = 1;\n            products.add(product);\n        } else{\n            product *= num;\n            products.add(product);\n        }\n        \n    }\n    \n    public int getProduct(int k) {\n        int size = products.size();\n        return k<size ? product/products.get(size-1-k): 0;\n    }\n}\n\n/**\n * Your ProductOfNumbers object will be instantiated and called as such:\n * ProductOfNumbers obj = new ProductOfNumbers();\n * obj.add(num);\n * int param_2 = obj.getProduct(k);\n */\n```
['Java']