LeetCode每日一题(1352. Product of the Last K Numbers)

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 integer num to the stream.
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.
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:

Input
[“ProductOfNumbers”,“add”,“add”,“add”,“add”,“add”,“getProduct”,“getProduct”,“getProduct”,“add”,“getProduct”]
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]

Output
[null,null,null,null,null,null,20,40,0,null,32]

Explanation
ProductOfNumbers productOfNumbers = new ProductOfNumbers();
productOfNumbers.add(3); // [3]
productOfNumbers.add(0); // [3,0]
productOfNumbers.add(2); // [3,0,2]
productOfNumbers.add(5); // [3,0,2,5]
productOfNumbers.add(4); // [3,0,2,5,4]
productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 _ 4 = 20
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 _ 5 _ 4 = 40
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 _ 2 _ 5 _ 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32

Constraints:

  • 0 <= num <= 100
  • 1 <= k <= 4 * 104
  • At most 4 * 104 calls will be made to add and getProduct.
  • The product of the stream at any point in time will fit in a 32-bit integer.

能想到用 prefix product 来做, 但是想不出来怎么解决 overflow 的问题, 后来看提示, 想通了, 一旦遇到 0, 前面的 prefix products 就都没有用了, 就要重新初始化, 最终做计算的时候, 如果 k >= prefix_products.len()则证明会包含 0, 这样直接返回 0 即可, 如果 k < prefix_products.len()我们就可以用 prefix_products[last] / prefix_products[len-k-1]来计算 last k 的 product。


struct ProductOfNumbers {
    products: Vec<i32>,
}

/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl ProductOfNumbers {
    fn new() -> Self {
        Self { products: vec![1] }
    }

    fn add(&mut self, num: i32) {
        if num == 0 {
            self.products = vec![1];
            return;
        }
        self.products.push(*self.products.last().unwrap() * num);
    }

    fn get_product(&self, k: i32) -> i32 {
        if k as usize >= self.products.len() {
            return 0;
        }
        *self.products.last().unwrap() / self.products[self.products.len() - k as usize - 1]
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值