forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1352.java
34 lines (28 loc) · 927 Bytes
/
_1352.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _1352 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product
*/
public static class ProductOfNumbers {
List<Integer> list;
public ProductOfNumbers() {
add(0);
}
public void add(int num) {
if (num > 0) {
list.add(list.get(list.size() - 1) * num);
} else {
list = new ArrayList<>();
list.add(1);
}
}
public int getProduct(int k) {
int size = list.size();
return k >= size ? 0 : (list.get(size - 1) / list.get(size - k - 1));
}
}
}
}