[LeetCode] Find Consecutive Integers from a Data Stream

2526. Find Consecutive Integers from a Data Stream

For a stream of integers, implement a data structure that checks if the last kvalue.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class DataStream {
int v,ma,c;
public:
DataStream(int value, int k) : v(value), ma(k), c(0) {}

bool consec(int num) {
if(v == num) c = min(c + 1, ma);
else c = 0;
return c == ma;
}
};

/**
* Your DataStream object will be instantiated and called as such:
* DataStream* obj = new DataStream(value, k);
* bool param_1 = obj->consec(num);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/08/PS/LeetCode/find-consecutive-integers-from-a-data-stream/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.