Leetcode Problem 1825. Finding MK Average

1825. Finding MK Average

Leetcode Solutions

Balancing Multisets Approach

  1. Initialize three multisets: left, mid, and right to represent the smallest k, middle m - 2k, and largest k elements, respectively.
  2. Initialize a variable sum to keep the sum of elements in mid.
  3. For each addElement operation: a. Add the new element to the appropriate set based on its value. b. Rebalance the sets if the sizes of left or right are not equal to k. c. Update the sum if elements are moved to or from the mid set.
  4. For the calculateMKAverage operation: a. If the total number of elements is less than m, return -1. b. Otherwise, return the floor value of sum divided by the size of mid.
UML Thumbnail

Deque and Multiset Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...