Solution: Finding MK Average
Explore how to design a custom data structure that maintains a sliding window of integer streams to compute the MK Average efficiently. Understand how to use a deque and three sorted lists to track lowest, middle, and highest values, and update sums for constant-time average computation. This lesson helps you grasp handling real-time data streams with balanced partitioning and binary search to optimize insertion and deletion operations.
We'll cover the following...
Statement
You are given two integers, m and k, and a stream of integers. Your task is to design and implement a data structure that efficiently calculates the MK Average for the stream.
To compute the MK Average, follow these steps:
Stream length check: If the stream contains fewer than
melements, return-1as the MK Average.Window selection: Otherwise, copy the last
melements of the stream to a separate container and remove the smallestkelements and the largestkelements from the container.Average calculation: Calculate the average of the remaining elements (rounded down to the nearest integer).
Implement the MKAverage class
MKAverage(int m, int k): Initializes the object with integersmandkand an empty stream.void addElement(int num): Adds the integernumto the stream.int calculateMKAverage(): Returns the current MK Average for the stream as described above, or-1if the stream contains fewer thanmelements.
Constraints:
mk*2...