2526. Find Consecutive Integers from a Data Stream LeetCode Solution

In this guide, you will get 2526. Find Consecutive Integers from a Data Stream LeetCode Solution with the best time and space complexity. The solution to Find Consecutive Integers from a Data Stream problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Consecutive Integers from a Data Stream solution in C++
  4. Find Consecutive Integers from a Data Stream solution in Java
  5. Find Consecutive Integers from a Data Stream solution in Python
  6. Additional Resources
2526. Find Consecutive Integers from a Data Stream LeetCode Solution image

Problem Statement of Find Consecutive Integers from a Data Stream

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 false.

Example 1:

Input
[“DataStream”, “consec”, “consec”, “consec”, “consec”]
[[4, 3], [4], [4], [4], [3]]
Output
[null, false, false, true, false]

Explanation
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
dataStream.consec(4); // Only 1 integer is parsed, so returns False.
dataStream.consec(4); // Only 2 integers are parsed.
// Since 2 is less than k, returns False.
dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
// Since 3 is not equal to value, it returns False.

Constraints:

1 <= value, num <= 109
1 <= k <= 105
At most 105 calls will be made to consec.

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(k)

2526. Find Consecutive Integers from a Data Stream LeetCode Solution in C++

class DataStream {
 public:
  DataStream(int value, int k) : value(value), k(k) {}

  bool consec(int num) {
    if (q.size() == k) {
      if (q.front() == value)
        --count;
      q.pop();
    }
    if (num == value)
      ++count;
    q.push(num);
    return count == k;
  }

 private:
  const int value;
  const int k;
  queue<int> q;
  int count = 0;
};
/* code provided by PROGIEZ */

2526. Find Consecutive Integers from a Data Stream LeetCode Solution in Java

class DataStream {
  public DataStream(int value, int k) {
    this.value = value;
    this.k = k;
  }

  public boolean consec(int num) {
    if (q.size() == k && q.poll() == value)
      --count;
    if (num == value)
      ++count;
    q.offer(num);
    return count == k;
  }

  private int value;
  private int k;
  private Queue<Integer> q = new ArrayDeque<>();
  private int count = 0;
}
// code provided by PROGIEZ

2526. Find Consecutive Integers from a Data Stream LeetCode Solution in Python

class DataStream:
  def __init__(self, value: int, k: int):
    self.value = value
    self.k = k
    self.q = deque()
    self.count = 0

  def consec(self, num: int) -> bool:
    if len(self.q) == self.k and self.q.popleft() == self.value:
      self.count -= 1
    if num == self.value:
      self.count += 1
    self.q.append(num)
    return self.count == self.k
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.