Skip to content

Commit 466334e

Browse files
refactor 1352
1 parent da92bcb commit 466334e

File tree

1 file changed

+3
-36
lines changed

1 file changed

+3
-36
lines changed

src/main/java/com/fishercoder/solutions/_1352.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,11 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 1352. Product of the Last K Numbers
8-
*
9-
* Implement the class ProductOfNumbers that supports two methods:
10-
* 1. add(int num)
11-
* Adds the number num to the back of the current list of numbers.
12-
* 2. getProduct(int k)
13-
* Returns the product of the last k numbers in the current list.
14-
* You can assume that always the current list has at least k numbers.
15-
* At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
16-
*
17-
* Example:
18-
* Input
19-
* ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
20-
* [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
21-
* Output
22-
* [null,null,null,null,null,null,20,40,0,null,32]
23-
* Explanation
24-
* ProductOfNumbers productOfNumbers = new ProductOfNumbers();
25-
* productOfNumbers.add(3); // [3]
26-
* productOfNumbers.add(0); // [3,0]
27-
* productOfNumbers.add(2); // [3,0,2]
28-
* productOfNumbers.add(5); // [3,0,2,5]
29-
* productOfNumbers.add(4); // [3,0,2,5,4]
30-
* productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
31-
* productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
32-
* productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
33-
* productOfNumbers.add(8); // [3,0,2,5,4,8]
34-
* productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
35-
*
36-
* Constraints:
37-
* There will be at most 40000 operations considering both add and getProduct.
38-
* 0 <= num <= 100
39-
* 1 <= k <= 40000
40-
* */
416
public class _1352 {
427
public static class Solution1 {
43-
/**credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product*/
8+
/**
9+
* credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product
10+
*/
4411
public static class ProductOfNumbers {
4512

4613
List<Integer> list;

0 commit comments

Comments
 (0)