Posted on

Description

Submission

class ProductOfNumbers {
    vector<int> pre;
    int n;
    int lastZero;
public:
    ProductOfNumbers() {
        pre.push_back(1);
        n = 0;
        lastZero = -1;
    }
    
    void add(int num) {
        if(num == 0) {
            lastZero = n;
            pre.push_back(1);
        } else {
            pre.push_back(pre.back() * num);
        }
        n++;
    }
    
    int getProduct(int k) {
        if(n-k <= lastZero) return 0;
        return pre[n] / pre[n-k];
    }
};

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers* obj = new ProductOfNumbers();
 * obj->add(num);
 * int param_2 = obj->getProduct(k);
 */

Leave a Reply

Your email address will not be published. Required fields are marked *