1825. Finding MK Average
https://leetcode.com/problems/finding-mk-average
Description
You are given two integers, m
and k
, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage for the stream.
The MKAverage can be calculated using these steps:
If the number of the elements in the stream is less than
m
you should consider the MKAverage to be-1
. Otherwise, copy the lastm
elements of the stream to a separate container.Remove the smallest
k
elements and the largestk
elements from the container.Calculate the average value for the rest of the elements rounded down to the nearest integer.
Implement the MKAverage
class:
MKAverage(int m, int k)
Initializes the MKAverage object with an empty stream and the two integersm
andk
.void addElement(int num)
Inserts a new elementnum
into the stream.int calculateMKAverage()
Calculates and returns the MKAverage for the current stream rounded down to the nearest integer.
Example 1:
Constraints:
3 <= m <= 105
1 <= k*2 < m
1 <= num <= 105
At most
105
calls will be made toaddElement
andcalculateMKAverage
.
ac
Last updated