#2526

Find Consecutive Integers from a Data Stream

specialist · 870 · lc medium +31 · 50.8% accepted · 339 likes · top 39%

Description

Implement the DataStream class for a stream of integers:

- DataStream(int value, int k) — initializes the stream with the target value and window size k.

- boolean consec(int num) — appends num to the stream and returns true if the last k integers are all equal to value; returns false if fewer than k integers have been added or the last k are not all value.

Example 1:

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

Example 2:

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.

Code

1
2
3
4
5
6
7
8
9
10
11
12