package leetcode;

/**
 * https://leetcode.com/problems/product-of-the-last-k-numbers/
 */
public class Problem1352 {
    static class ProductOfNumbers {
        public ProductOfNumbers() {
        }

        public void add(int num) {
            // TODO
        }

        public int getProduct(int k) {
            // TODO
            return 0;
        }
    }

    public static void main(String[] args) {
        ProductOfNumbers productOfNumbers = new ProductOfNumbers();
        productOfNumbers.add(3); // [3]
        productOfNumbers.add(0); // [3,0]
        productOfNumbers.add(2); // [3,0,2]
        productOfNumbers.add(5); // [3,0,2,5]
        productOfNumbers.add(4); // [3,0,2,5,4]
        System.out.println(productOfNumbers.getProduct(2)); // return 20. The product of the last 2 numbers is 5 * 4 = 20
        System.out.println(productOfNumbers.getProduct(3)); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
        System.out.println(productOfNumbers.getProduct(4)); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
        productOfNumbers.add(8); // [3,0,2,5,4,8]
        System.out.println(productOfNumbers.getProduct(2)); // return 32. The product of the last 2 numbers is 4 * 8 = 32
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -212,100 +212,564 @@
    p
-ublic ProductOfNumbers() %7B%0A        %7D%0A%0A        public void add(int num) %7B%0A            // TODO
+rivate final List%3CInteger%3E list = new ArrayList%3C%3E();%0A        private int product = 1;%0A%0A        public ProductOfNumbers() %7B%0A        %7D%0A%0A        public void add(int num) %7B%0A            if (num == 0) %7B%0A                product = 0;%0A            %7D else if (list.isEmpty()) %7B%0A                product = num;%0A            %7D else if (list.size() - 1 %3E 0) %7B%0A                if (list.get(list.size() - 1) == 0) %7B%0A                    product = num;%0A                %7D else %7B%0A                    product *= num;%0A                %7D%0A            %7D%0A            list.add(product);
 %0A   

Update problem 1352