Advertisement

Find Consecutive Integers from a Data Stream - LeetCode 2526 Solution

Find Consecutive Integers from a Data Stream - Complete Solution Guide

Find Consecutive Integers from a Data Stream is LeetCode problem 2526, 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

For a stream of integers, implement a data structure that checks if the last k integers parsed in the stream are equal to value . Implement the DataStream class: DataStream(int value, int k) Initializes the object with an empty integer stream and the two integers value and k . boolean consec(int num) Adds num to the stream of integers. Returns true if the last k integers are equal to value , and false otherwise. If there are less than k integers, the condition does not hold true, so returns fals

Detailed Explanation

The problem requires us to implement a data structure that processes a stream of integers. The `DataStream` class should be initialized with a `value` and a `k`. The `consec(num)` method adds a new integer `num` to the stream and checks if the last `k` integers in the stream are all equal to the initialized `value`. It returns `true` if the last `k` integers are all equal to `value`, and `false` otherwise. If the stream has fewer than `k` integers, the method should return `false`.

Solution Approach

The solution uses a counter (`count`) to keep track of the number of consecutive integers equal to the target `value` at the end of the stream. Each time a new number `num` is added to the stream using the `consec` method, we check if `num` is equal to `value`. If it is, we increment the counter. Otherwise, we reset the counter to 0. Finally, we return `true` if the counter is greater than or equal to `k`, indicating that the last `k` integers are equal to `value`, and `false` otherwise.

Step-by-Step Algorithm

  1. Step 1: Initialize the `DataStream` object with the target `value` and required consecutive count `k`.
  2. Step 2: Initialize a counter `count` to 0. This counter will store the number of consecutive occurrences of `value` at the end of the data stream.
  3. Step 3: In the `consec(num)` method, check if the input `num` is equal to the target `value`.
  4. Step 4: If `num == value`, increment the `count`. Otherwise, reset the `count` to 0 because the consecutive sequence is broken.
  5. Step 5: Return `true` if the `count` is greater than or equal to `k` (meaning the last `k` numbers are consecutively equal to value), and `false` otherwise.

Key Insights

  • Insight 1: We only need to keep track of the count of consecutive occurrences of the target value at the end of the stream.
  • Insight 2: A simple counter is sufficient to solve this problem efficiently because we don't need to store the actual stream of numbers.
  • Insight 3: Resetting the counter when a different number is encountered is crucial for maintaining the consecutive nature of the check.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, Design, Queue, Counting, Data Stream.

Companies

Asked at: Intel.