package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/product-of-the-last-k-numbers/
 */
public class Problem1352 {
    static class ProductOfNumbers {
        private final List<Integer> list = new ArrayList<>();
        private int product = 1;

        public ProductOfNumbers() {
        }

        public void add(int num) {
            if (num == 0) {
                product = 0;
            } else if (list.isEmpty()) {
                product = num;
            } else if (list.size() - 1 > 0) {
                if (list.get(list.size() - 1) == 0) {
                    product = num;
                } else {
                    product *= num;
                }
            }
            list.add(product);
        }

        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
    }
}

@@ -264,41 +264,8 @@
 %3E();
-%0A        private int product = 1;
 %0A%0A  
@@ -392,19 +392,20 @@
     
-product = 0
+list.clear()
 ;%0A  
@@ -455,37 +455,37 @@
             
-product = 
+list.add(
 num
+)
 ;%0A          
@@ -508,20 +508,16 @@
 t.size()
- - 1
  %3E 0) %7B%0A
@@ -594,21 +594,20 @@
     
-product = num
+list.clear()
 ;%0A  
@@ -653,22 +653,49 @@
     
-product
+list.add(list.get(list.size() - 1)
  *
-=
  num
+)
 ;%0A  
@@ -728,39 +728,8 @@
   %7D%0A
-            list.add(product);%0A
     
@@ -790,935 +790,251 @@
     
-// TODO%0A            return 0;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        ProductOfNumbers productOfNumbers = new ProductOfNumbers();%0A        productOfNumbers.add(3); // %5B3%5D%0A        productOfNumbers.add(0); // %5B3,0%5D%0A        productOfNumbers.add(2); // %5B3,0,2%5D%0A        productOfNumbers.add(5); // %5B3,0,2,5%5D%0A        productOfNumbers.add(4); // %5B3,0,2,5,4%5D%0A        System.out.println(productOfNumbers.getProduct(2)); // return 20. The product of the last 2 numbers is 5 * 4 = 20%0A        System.out.println(productOfNumbers.getProduct(3)); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40%0A        System.out.println(productOfNumbers.getProduct(4)); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0%0A        productOfNumbers.add(8); // %5B3,0,2,5,4,8%5D%0A        System.out.println(productOfNumbers.getProduct(2)); // return 32. The product of the last 2 numbers is 4 * 8 = 32
+if (k %3E list.size()) %7B%0A                return 0;%0A            %7D else if (k == list.size()) %7B%0A                return list.get(list.size() - 1);%0A            %7D%0A            return list.get(list.size() - 1) / list.get(list.size() - k - 1);%0A        %7D
 %0A   

Solve problem 1352