1352. Product of the Last K Numbers
https://leetcode.com/problems/product-of-the-last-k-numbers
Description
Design an algorithm that accepts a stream of integers and retrieves the product of the last k
integers of the stream.
Implement the ProductOfNumbers
class:
ProductOfNumbers()
Initializes the object with an empty stream.void add(int num)
Appends the integernum
to the stream.int getProduct(int k)
Returns the product of the lastk
numbers in the current list. You can assume that always the current list has at leastk
numbers.
The test cases are generated so that, at any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
Example:
Constraints:
0 <= num <= 100
1 <= k <= 4 * 104
At most
4 * 104
calls will be made toadd
andgetProduct
.The product of the stream at any point in time will fit in a 32-bit integer.
ac
Last updated