1825. Finding MK Average Hard

@problem@discussion
#Design#Queue#Heap (Priority Queue)#Data Stream#Ordered Set



1/**
2 * [1825] Finding MK Average
3 *
4 * 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.
5 * The MKAverage can be calculated using these steps:
6 * <ol>
7 * 	If the number of the elements in the stream is less than m you should consider the MKAverage to be -1. Otherwise, copy the last m elements of the stream to a separate container.
8 * 	Remove the smallest k elements and the largest k elements from the container.
9 * 	Calculate the average value for the rest of the elements rounded down to the nearest integer.
10 * </ol>
11 * Implement the MKAverage class:
12 * 
13 * 	MKAverage(int m, int k) Initializes the MKAverage object with an empty stream and the two integers m and k.
14 * 	void addElement(int num) Inserts a new element num into the stream.
15 * 	int calculateMKAverage() Calculates and returns the MKAverage for the current stream rounded down to the nearest integer.
16 * 
17 *  
18 * Example 1:
19 * 
20 * Input
21 * ["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]
22 * [[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]
23 * Output
24 * [null, null, null, -1, null, 3, null, null, null, 5]
25 * Explanation
26 * MKAverage obj = new MKAverage(3, 1); 
27 * obj.addElement(3);        // current elements are [3]
28 * obj.addElement(1);        // current elements are [3,1]
29 * obj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.
30 * obj.addElement(10);       // current elements are [3,1,10]
31 * obj.calculateMKAverage(); // The last 3 elements are [3,1,10].
32 *                           // After removing smallest and largest 1 element the container will be [3].
33 *                           // The average of [3] equals 3/1 = 3, return 3
34 * obj.addElement(5);        // current elements are [3,1,10,5]
35 * obj.addElement(5);        // current elements are [3,1,10,5,5]
36 * obj.addElement(5);        // current elements are [3,1,10,5,5,5]
37 * obj.calculateMKAverage(); // The last 3 elements are [5,5,5].
38 *                           // After removing smallest and largest 1 element the container will be [5].
39 *                           // The average of [5] equals 5/1 = 5, return 5
40 * 
41 *  
42 * Constraints:
43 * 
44 * 	3 <= m <= 10^5
45 * 	1 <= k*2 < m
46 * 	1 <= num <= 10^5
47 * 	At most 10^5 calls will be made to addElement and calculateMKAverage.
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/finding-mk-average/
53// discuss: https://leetcode.com/problems/finding-mk-average/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57struct MKAverage {
58        false
59    }
60
61
62/** 
63 * `&self` means the method takes an immutable reference.
64 * If you need a mutable reference, change it to `&mut self` instead.
65 */
66impl MKAverage {
67
68    fn new(m: i32, k: i32) -> Self {
69        
70    }
71    
72    fn add_element(&self, num: i32) {
73        
74    }
75    
76    fn calculate_mk_average(&self) -> i32 {
77        
78    }
79}
80
81/**
82 * Your MKAverage object will be instantiated and called as such:
83 * let obj = MKAverage::new(m, k);
84 * obj.add_element(num);
85 * let ret_2: i32 = obj.calculate_mk_average();
86 */
87
88// submission codes end
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn test_1825() {
96    }
97}
98


Back
© 2025 bowen.ge All Rights Reserved.