|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1352. Product of the Last K Numbers |
| 5 | + * |
| 6 | + * Implement the class ProductOfNumbers that supports two methods: |
| 7 | + * 1. add(int num) |
| 8 | + * Adds the number num to the back of the current list of numbers. |
| 9 | + * 2. getProduct(int k) |
| 10 | + * Returns the product of the last k numbers in the current list. |
| 11 | + * You can assume that always the current list has at least k numbers. |
| 12 | + * At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing. |
| 13 | + * |
| 14 | + * Example: |
| 15 | + * Input |
| 16 | + * ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] |
| 17 | + * [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] |
| 18 | + * Output |
| 19 | + * [null,null,null,null,null,null,20,40,0,null,32] |
| 20 | + * Explanation |
| 21 | + * ProductOfNumbers productOfNumbers = new ProductOfNumbers(); |
| 22 | + * productOfNumbers.add(3); // [3] |
| 23 | + * productOfNumbers.add(0); // [3,0] |
| 24 | + * productOfNumbers.add(2); // [3,0,2] |
| 25 | + * productOfNumbers.add(5); // [3,0,2,5] |
| 26 | + * productOfNumbers.add(4); // [3,0,2,5,4] |
| 27 | + * productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 |
| 28 | + * productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 |
| 29 | + * productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 |
| 30 | + * productOfNumbers.add(8); // [3,0,2,5,4,8] |
| 31 | + * productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * There will be at most 40000 operations considering both add and getProduct. |
| 35 | + * 0 <= num <= 100 |
| 36 | + * 1 <= k <= 40000 |
| 37 | + * */ |
| 38 | +public class _1352 { |
| 39 | + public static class Solution1 { |
| 40 | + public static class ProductOfNumbers { |
| 41 | + |
| 42 | + public ProductOfNumbers() { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + public void add(int num) { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public int getProduct(int k) { |
| 51 | + return -1; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments