Leetcode 2526: Find Consecutive Integers from a Data Stream

grid47
grid47
Exploring patterns and algorithms
Feb 28, 2024 5 min read

You need to implement a DataStream class that processes a stream of integers and checks whether the last k integers in the stream are equal to a specified value.
Problem
Approach
Steps
Complexity
Input: The input consists of two integers: `value`, the target value to check for, and `k`, the number of consecutive integers to be checked.
Example: [3, 4]
Constraints:
• 1 <= value, num <= 10^9
• 1 <= k <= 10^5
• At most 10^5 calls will be made to consec.
Output: The output should be a boolean value: `true` if the last k integers are equal to the target value, otherwise `false`. If fewer than k integers have been parsed, return `false`.
Example: false
Constraints:
• The output is a boolean value representing the result of the consec method.
Goal: Check if the last k integers in the stream are equal to the target value.
Steps:
• Store the target value and the integer k.
• Maintain a count of consecutive integers equal to the target value.
• For each call to consec(), add the integer to the stream and update the count. If the count reaches k, return true. If not, return false.
Goal: Ensure that all input values respect the given constraints for value, num, and k.
Steps:
• 1 <= value, num <= 10^9
• 1 <= k <= 10^5
• At most 10^5 calls will be made to consec.
Assumptions:
• The input values are within the given constraints.
• The number of consec calls will not exceed 10^5.
Input: [3, 4], [3], [3], [3], [4]
Explanation: The DataStream object is initialized with value 3 and k = 4. It returns false until 4 consecutive values of 3 are encountered.

Input: [5, 2], [5], [5]
Explanation: The DataStream object is initialized with value 5 and k = 2. It returns false initially, then returns true after two consecutive values of 5.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus