Class DataStream
java.lang.Object
g2501_2600.s2526_find_consecutive_integers_from_a_data_stream.DataStream
2526 - Find Consecutive Integers from a Data Stream.<p>Medium</p>
<p>For a stream of integers, implement a data structure that checks if the last <code>k</code> integers parsed in the stream are <strong>equal</strong> to <code>value</code>.</p>
<p>Implement the <strong>DataStream</strong> class:</p>
<ul>
<li><code>DataStream(int value, int k)</code> Initializes the object with an empty integer stream and the two integers <code>value</code> and <code>k</code>.</li>
<li><code>boolean consec(int num)</code> Adds <code>num</code> to the stream of integers. Returns <code>true</code> if the last <code>k</code> integers are equal to <code>value</code>, and <code>false</code> otherwise. If there are less than <code>k</code> integers, the condition does not hold true, so returns <code>false</code>.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input</strong> [“DataStream”, “consec”, “consec”, “consec”, “consec”]</p>
<p>[[4, 3], [4], [4], [4], [3]]</p>
<p><strong>Output:</strong> [null, false, false, true, false]</p>
<p><strong>Explanation:</strong></p>
<pre><code> 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></pre>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= value, num <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
<li>At most <code>10<sup>5</sup></code> calls will be made to <code>consec</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
DataStream
public DataStream(int value, int k)
-
-
Method Details
-
consec
public boolean consec(int num)
-