Advertisement

Product of the Last K Numbers - LeetCode 1352 Solution

Product of the Last K Numbers - Complete Solution Guide

Product of the Last K Numbers is LeetCode problem 1352, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem requires designing a data structure (ProductOfNumbers class) that can efficiently handle a stream of integers. It needs to support two operations: `add(num)`, which appends a new integer to the stream, and `getProduct(k)`, which returns the product of the last `k` numbers added to the stream. A crucial constraint is that the product of any contiguous sequence will fit within a 32-bit integer. The follow-up asks to achieve O(1) time complexity for both `add` and `getProduct` methods.

Solution Approach

The solution uses a list (or vector/array) called `products` to store the prefix products. `products[i]` holds the product of all numbers added so far up to index `i`. When a new number `num` is added, if it's 0, the entire `products` list is reset to `[1]`. Otherwise, the product `products[-1] * num` is appended to the `products` list. When `getProduct(k)` is called, the product of the last `k` numbers can be calculated in O(1) time by dividing the last element of the `products` list by the element `k` positions before the end. A special case is handled if `k` is greater than or equal to the current size of `products`, which means that some previous element had a '0', so it returns 0.

Step-by-Step Algorithm

  1. Step 1: Initialize a list `products` with `[1]`. The initial value '1' handles cases where getProduct is called before any numbers are added and simplifies the calculations.
  2. Step 2: In `add(num)`, check if `num` is 0. If it is, clear the `products` list and reset it to `[1]` because any subsequent product including this zero will be zero. If `num` is not zero, append `products[-1] * num` to the `products` list.
  3. Step 3: In `getProduct(k)`, check if `k` is greater than or equal to the size of the `products` list. If it is, return 0, implying that there was a zero within the last `k` elements added.
  4. Step 4: Otherwise, return `products[-1] // products[-(k + 1)]` (Python) or `products.get(products.size() - 1) / products.get(products.size() - (k + 1))` (Java/C++). This calculates the product of the last `k` numbers by dividing the cumulative product by the product of all elements before the last `k` elements.

Key Insights

  • Insight 1: Prefix products allow calculating the product of a range in O(1) time.
  • Insight 2: The presence of zero significantly impacts the calculation since any sub-array containing zero will have a product of zero.
  • Insight 3: When a zero is encountered, the accumulated product series must be reset to handle subsequent `getProduct` requests properly.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Array, Math, Design, Data Stream, Prefix Sum.

Companies

Asked at: Target, Tekion.