1352. Product of the Last K Numbers Medium

@problem@discussion
#Array#Math#Design#Queue#Data Stream



1/**
2 * [1352] Product of the Last K Numbers
3 *
4 * Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream.
5 * Implement the ProductOfNumbers class:
6 * 
7 * 	ProductOfNumbers() Initializes the object with an empty stream.
8 * 	void add(int num) Appends the integer num to the stream.
9 * 	int 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.
10 * 
11 * 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.
12 *  
13 * Example:
14 * 
15 * Input
16 * ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
17 * [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
18 * Output
19 * [null,null,null,null,null,null,20,40,0,null,32]
20 * Explanation
21 * ProductOfNumbers productOfNumbers = new ProductOfNumbers();
22 * productOfNumbers.add(3);        // [3]
23 * productOfNumbers.add(0);        // [3,0]
24 * productOfNumbers.add(2);        // [3,0,2]
25 * productOfNumbers.add(5);        // [3,0,2,5]
26 * productOfNumbers.add(4);        // [3,0,2,5,4]
27 * productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
28 * productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
29 * productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
30 * productOfNumbers.add(8);        // [3,0,2,5,4,8]
31 * productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 
32 * 
33 *  
34 * Constraints:
35 * 
36 * 	0 <= num <= 100
37 * 	1 <= k <= 4 * 10^4
38 * 	At most 4 * 10^4 calls will be made to add and getProduct.
39 * 	The product of the stream at any point in time will fit in a 32-bit integer.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/product-of-the-last-k-numbers/
45// discuss: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49struct ProductOfNumbers {
50        false
51    }
52
53
54/** 
55 * `&self` means the method takes an immutable reference.
56 * If you need a mutable reference, change it to `&mut self` instead.
57 */
58impl ProductOfNumbers {
59
60    fn new() -> Self {
61        
62    }
63    
64    fn add(&self, num: i32) {
65        
66    }
67    
68    fn get_product(&self, k: i32) -> i32 {
69        
70    }
71}
72
73/**
74 * Your ProductOfNumbers object will be instantiated and called as such:
75 * let obj = ProductOfNumbers::new();
76 * obj.add(num);
77 * let ret_2: i32 = obj.get_product(k);
78 */
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_1352() {
88    }
89}
90


Back
© 2025 bowen.ge All Rights Reserved.