Skip to content

Commit 6a4ed92

Browse files
authored
Update Product of the Last K Numbers.java
1 parent 58878fd commit 6a4ed92

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Medium/Product of the Last K Numbers.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
class ProductOfNumbers {
2-
List<Integer> list;
2+
3+
private List<Integer> list;
4+
35
public ProductOfNumbers() {
6+
this.list = new ArrayList<>();
47
add(0);
58
}
69

710
public void add(int num) {
811
if (num > 0) {
912
list.add(list.get(list.size() - 1) * num);
10-
}
11-
else {
13+
} else {
1214
list = new ArrayList<>();
1315
list.add(1);
1416
}
1517
}
1618

1719
public int getProduct(int k) {
18-
return k < list.size() ? list.get(list.size() - 1) / list.get(list.size() - k - 1) : 0;
20+
int n = list.size();
21+
return k < n ? list.get(n - 1) / list.get(n - k - 1) : 0;
1922
}
2023
}
2124

0 commit comments

Comments
 (0)