Skip to content

Commit 9676d29

Browse files
authored
Create 1352-product-of-the-last-k-numbers.js
1 parent 236397b commit 9676d29

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1352-product-of-the-last-k-numbers.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode id=1352 lang=javascript
3+
*
4+
* [1352] Product of the Last K Numbers
5+
*/
6+
7+
// @lc code=start
8+
9+
const ProductOfNumbers = function() {
10+
this.sum = [1]
11+
};
12+
13+
/**
14+
* @param {number} num
15+
* @return {void}
16+
*/
17+
ProductOfNumbers.prototype.add = function(num) {
18+
if(num > 0) {
19+
this.sum.push(this.sum[this.sum.length - 1] * num)
20+
} else {
21+
this.sum = [1]
22+
}
23+
};
24+
25+
/**
26+
* @param {number} k
27+
* @return {number}
28+
*/
29+
ProductOfNumbers.prototype.getProduct = function(k) {
30+
const len = this.sum.length
31+
return k < len ? this.sum[len - 1] / this.sum[len - 1 - k] : 0
32+
};
33+
34+
/**
35+
* Your ProductOfNumbers object will be instantiated and called as such:
36+
* var obj = new ProductOfNumbers()
37+
* obj.add(num)
38+
* var param_2 = obj.getProduct(k)
39+
*/

0 commit comments

Comments
 (0)