Description
Implement the class ProductOfNumbers that supports two methods:
- add(int num)
Adds the number num to the back of the current list of numbers.
- getProduct(int k)
Returns the product of the last k numbers in the current list.
You can assume that always the current list has at least k numbers.
At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
Example
1 | Input |
Constraints:
- There will be at most 40000 operations considering both add and getProduct.
- 0 <= num <= 100
- 1 <= k <= 40000
Solution
- Time Complexity:
- Space Complexity:
1 | class ProductOfNumbers { |