Beats 99.69% | Array | List | Solution for LeetCode#1352
IntuitionThe problem requires us to create a data structure that supports adding numbers and getting the product of the last k numbers. We need to efficiently c
![image.png](https://assets.leetcode.com/users/images/4d58c326-3c48-4346-88da-d292cca710c6_1739500076.9601388.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to create a data structure that supports adding numbers and getting the product of the last k numbers. We need to efficiently calculate the product, especially considering the possibility of zeros resetting the sequence.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize: Use a List<Integer> to store the cumulative product of the numbers added.
2. Add Operation: When adding a number, multiply it with the last cumulative product in the list. If the number is zero, reset the list as zero invalidates all previous products.
3. Get Product: To get the product of the last k numbers, divide the last cumulative product by the cumulative product k steps back. Handle the case where k exceeds the number of elements in the list by returning 0.

# Complexity
- Time complexity:
    - add(num): $$O(1)$$ - Adding a number involves a single operation of updating the list.
    - getProduct(k): $$O(1)$$ - Getting the product involves a single division operation.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$, Space used by the list to store cumulative products, where n is the number of elements added.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class ProductOfNumbers {
    List<Integer> listProduct;

    public ProductOfNumbers() {
        listProduct=new ArrayList();
        listProduct.add(1);
    }
    
    public void add(int num) {
        if(num==0){
            listProduct=new ArrayList();
            listProduct.add(1);
        }else{
            listProduct.add(listProduct.get(listProduct.size()-1)*num);    
        }    
    }
    
    public int getProduct(int k) {
        if(k>listProduct.size()-1){
            return 0;
        }
        else{
            return listProduct.get(listProduct.size()-1)/listProduct.get(listProduct.size()-1-k);
        }
        
    }
}

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers obj = new ProductOfNumbers();
 * obj.add(num);
 * int param_2 = obj.getProduct(k);
 */
```
```python []
class ProductOfNumbers(object):
    listProd=[]
    def __init__(self):        
        self.listProd.append(1)

    def add(self, num):
        """
        :type num: int
        :rtype: None
        """
        if num==0:
            self.listProd=[]  
            self.listProd.append(1)
        else:
            self.listProd.append(self.listProd[-1]*num)
        

    def getProduct(self, k):
        """
        :type k: int
        :rtype: int
        """
        if k>len(self.listProd)-1:
            return 0
        else:
            return self.listProd[-1]/self.listProd[-1-k]
        


# Your ProductOfNumbers object will be instantiated and called as such:
# obj = ProductOfNumbers()
# obj.add(num)
# param_2 = obj.getProduct(k)
```





['Python', 'Java', 'Python3']