question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
new-21-game
Would someone help me out? example3 case failed.
would-someone-help-me-out-example3-case-wt2pd
My intuitive idea is that \nstep1, get the total number ways (TOTAL) to get score>=K;\nstep2, and get the total number ways (COUNT)to get score <=N\nand result
beyourself
NORMAL
2018-05-20T06:33:51.931499+00:00
2018-05-20T06:33:51.931499+00:00
370
false
My intuitive idea is that \nstep1, get the total number ways (TOTAL) to get score>=K;\nstep2, and get the total number ways (COUNT)to get score <=N\nand resulting probability is(TOTAL / COUNT).\nHere is my brute-force code(I think memorization could be applied...let us just ignore it. Just focus on the correctness of ...
1
0
[]
1
new-21-game
Python O(n) solution
python-on-solution-by-hexadecimal-cl59
It took me more than 30 minutes to figure out why I was "wrong" at Example 3...\n```class Solution:\n def new21Game(self, N, K, W):\n """\n :ty
hexadecimal
NORMAL
2018-05-20T03:24:33.266090+00:00
2018-05-20T03:24:33.266090+00:00
751
false
It took me more than 30 minutes to figure out why I was "wrong" at Example 3...\n```class Solution:\n def new21Game(self, N, K, W):\n """\n :type N: int\n :type K: int\n :type W: int\n :rtype: float\n """\n d=[0]*(K+max(W,N)+10)\n for i in range(K,N+1):\n ...
1
0
[]
1
finding-mk-average
C++ Balance 3 Multisets
c-balance-3-multisets-by-votrubac-h1z3
A bit lengthy; the idea is to keep track of the left, mid, and right sets. When removing or adding numbers, we reballance these sets so that the size of left an
votrubac
NORMAL
2021-04-11T05:30:25.568829+00:00
2021-04-11T05:47:30.701111+00:00
10,569
false
A bit lengthy; the idea is to keep track of the `left`, `mid`, and `right` sets. When removing or adding numbers, we reballance these sets so that the size of `left` and `right` stays `k`.\n\nAlso, we keep track of `sum` for elements in `mid`. That way, the calculate function is O(1).\n \nFinally, to track the current ...
127
1
[]
21
finding-mk-average
Java O(logN), O(1) time. beat 100%. SortedList implementation.
java-ologn-o1-time-beat-100-sortedlist-i-qcxc
Java doesn\'t has SortedList like python. So manually implemented one.\nThe idea is to maintain 3 SortedList. Every time when add new element, remove old elemen
allenlipeng47
NORMAL
2021-04-11T07:28:14.797017+00:00
2021-04-11T07:31:30.663106+00:00
4,791
false
Java doesn\'t has ```SortedList``` like python. So manually implemented one.\nThe idea is to maintain 3 SortedList. Every time when add new element, remove old element, keep 3 ```SortedList``` size as required.\nTotal size of ```l1```, ```l2```, ```l3``` is ```m```, ```l1```, ```l3``` has length of ```k```\n```\npublic...
69
0
[]
3
finding-mk-average
Python3 solution w/ SortedList, O(logM) add, O(1) calculate
python3-solution-w-sortedlist-ologm-add-gao8n
python3 doesn\'t have a built-in order statistic tree or even a basic BST and that may be why sortedcontainers is one of the few third-party libraries allowed.\
chuan-chih
NORMAL
2021-04-11T04:35:31.428924+00:00
2021-04-11T04:45:46.451031+00:00
5,218
false
python3 doesn\'t have a built-in [order statistic tree](https://en.wikipedia.org/wiki/Order_statistic_tree) or even a basic BST and that may be why `sortedcontainers` is one of the few third-party libraries allowed.\n\nAnyway, with a [SortedList](http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html) this sol...
60
1
['Queue', 'Python3']
8
finding-mk-average
Clean Java with 3 TreeMaps
clean-java-with-3-treemaps-by-fang2018-ldg8
Sadly, there isn\'t a built-in multiset in Java, therefore we have to play around with treemap. \n\nclass MKAverage {\n TreeMap<Integer, Integer> top = new T
fang2018
NORMAL
2021-04-11T05:43:08.802309+00:00
2021-04-11T05:48:04.995994+00:00
4,696
false
Sadly, there isn\'t a built-in multiset in Java, therefore we have to play around with treemap. \n```\nclass MKAverage {\n TreeMap<Integer, Integer> top = new TreeMap<>(), middle = new TreeMap<>(), bottom = new TreeMap<>();\n Queue<Integer> q = new LinkedList<>();\n long middleSum;\n int m, k;\n int coun...
53
1
[]
12
finding-mk-average
[Python3] Fenwick tree
python3-fenwick-tree-by-ye15-7n7f
\n\nclass Fenwick: \n\n def __init__(self, n: int):\n self.nums = [0]*(n+1)\n\n def sum(self, k: int) -> int: \n k += 1\n ans = 0\n
ye15
NORMAL
2021-04-11T04:01:41.449823+00:00
2021-04-11T04:01:41.449850+00:00
6,647
false
\n```\nclass Fenwick: \n\n def __init__(self, n: int):\n self.nums = [0]*(n+1)\n\n def sum(self, k: int) -> int: \n k += 1\n ans = 0\n while k:\n ans += self.nums[k]\n k &= k-1 # unset last set bit \n return ans\n\n def add(self, k: int, x: int) -> None:...
53
2
['Python3']
7
finding-mk-average
C++ 3 multisets
c-3-multisets-by-lzl124631x-hhar
See my latest update in repo LeetCode\n\n## Solution 1. 3 Multisets\n\n Use 3 multiset<int> to track the top k, bottom k and middle elements.\n Use queue<int> q
lzl124631x
NORMAL
2021-04-11T04:00:52.940852+00:00
2021-04-12T04:24:14.305750+00:00
3,331
false
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. 3 Multisets\n\n* Use 3 `multiset<int>` to track the top `k`, bottom `k` and middle elements.\n* Use `queue<int> q` to track the current `m` numbers.\n* Use `sum` to track the sum of numbers in `mid`.\n\nPlease see comments...
42
2
[]
3
finding-mk-average
[Python] 4 heaps, one list, O(n log n), explained
python-4-heaps-one-list-on-log-n-explain-394b
The idea is similar to problem 480. Sliding Window Median, here we also have sliding window, but we need to find not median, but some other statistics: sum of s
dbabichev
NORMAL
2021-04-11T21:49:18.183373+00:00
2021-04-15T09:08:34.447190+00:00
3,583
false
The idea is similar to problem **480. Sliding Window Median**, here we also have sliding window, but we need to find not median, but some other statistics: sum of some middle elements in sorted window. Imagine for the purpose of example that `m = 7` and `k = 2`. Then we have widow of size `7`: `a1 < a2 < a3 < a4 < a5 <...
38
0
['Heap (Priority Queue)']
7
finding-mk-average
Java one treemap and deque solution
java-one-treemap-and-deque-solution-by-t-e17q
Maintain a deque so we can remove the m+1 element. Maintain current sum and tot elements.\n- Keep a treemap and remove m+1 element on each add.\n- To compute av
techguy
NORMAL
2021-08-19T16:10:43.335224+00:00
2021-08-19T16:11:14.309944+00:00
637
false
- Maintain a deque so we can remove the `m+1` element. Maintain current sum and tot elements.\n- Keep a treemap and remove `m+1` element on each `add`.\n- To compute avg get low `k` elements, and top `k` elements, and subtract from current sum.\n\n```\nclass MKAverage {\n\n int tot = 0;\n int sum = 0;\n int m ...
17
0
[]
7
finding-mk-average
Java | Queue + TreeMap | Explained, Example
java-queue-treemap-explained-example-by-0b4dl
Haven\'t seen this in the comments so I\'ll just go ahead with some bullet points.\n\n We need to be able to access the first and last k elements (of the last m
ig-rib
NORMAL
2022-11-26T22:56:34.738264+00:00
2022-11-26T22:56:34.738291+00:00
912
false
Haven\'t seen this in the comments so I\'ll just go ahead with some bullet points.\n\n* We need to be able to access the first and last k elements (of the last m elements) of the stream. If there was something as a SortedQueue in Java it would be cool, but there isn\'t, so **TreeMap** was all I could come up with, in o...
15
0
['Tree', 'Queue', 'Java']
2
finding-mk-average
Java Solution | PriorityQueue + Lazy Deletion | O(log M) add, O(1) calculate | Detailed Explanations
java-solution-priorityqueue-lazy-deletio-hlod
The structure of this solution may be a little complex, but easy to understand. Here, I keep 4 priority_queues to record the smallest k elements, (m -k2) elment
cdhcs1516
NORMAL
2021-04-11T05:12:45.990856+00:00
2021-04-11T05:13:39.533382+00:00
2,878
false
The structure of this solution may be a little complex, but easy to understand. Here, I keep 4 priority_queues to record the smallest k elements, (m -k*2) elments in middle window and the largest k elements. For each operation, I just need to check which priority_queue the queue_in number and queue_out number locates, ...
14
3
['Hash Table', 'Heap (Priority Queue)', 'Java']
3
finding-mk-average
C++ and multisets
c-and-multisets-by-motorbreathing-m1ko
\nclass MKAverage {\n queue<int> q;\n multiset<int> minheap;\n multiset<int> midheap;\n multiset<int> maxheap;\n int m;\n int k;\n int coun
motorbreathing
NORMAL
2022-06-18T10:27:24.879887+00:00
2022-06-18T10:27:24.879927+00:00
589
false
```\nclass MKAverage {\n queue<int> q;\n multiset<int> minheap;\n multiset<int> midheap;\n multiset<int> maxheap;\n int m;\n int k;\n int count;\n long sum;\npublic:\n MKAverage(int m, int k) {\n this->m = m;\n this->k = k;\n count = 0;\n sum = 0;\n }\n \n ...
12
0
[]
1
finding-mk-average
[Java] Fenwick Tree + BinarySearch
java-fenwick-tree-binarysearch-by-66brot-mj54
Idea : \n1. We can have a queue to maintain m elements\n2. Use two Fenwick tree, 1 for count and 1 for prefix sum\n3. Do 2 times binary search for the first k e
66brother
NORMAL
2021-04-11T04:01:16.968660+00:00
2021-04-12T06:57:39.638150+00:00
1,555
false
Idea : \n1. We can have a queue to maintain m elements\n2. Use two Fenwick tree, 1 for count and 1 for prefix sum\n3. Do 2 times binary search for the first k elements and the last k elements by using the count from our first fenwick tree\n4. We can get the sum by subtrating the sum of first k elements and sum of last ...
12
0
[]
3
finding-mk-average
[Python] SortedList solution
python-sortedlist-solution-by-oystermax-3n8f
There is a sortedcontainers package for Python (link), which I think is a good alternative for red-black tree or ordered map for python players.\nUsing SortedLi
oystermax
NORMAL
2021-05-25T21:59:37.099283+00:00
2021-06-01T22:06:26.116785+00:00
914
false
There is a `sortedcontainers` package for Python ([link](http://www.grantjenks.com/docs/sortedcontainers/index.html)), which I think is a good alternative for red-black tree or ordered map for python players.\nUsing `SortedList`, it takes `~ O(logn)` time for both remove and insert. (Since `SortedList` is based on list...
11
0
['Python']
1
finding-mk-average
python | 100% faster | bst solution explained
python-100-faster-bst-solution-explained-7bpn
So the challenge here is how can we quickly sum the k-largest and k-smallest elements, and I want to talk about how I did that with a binary search tree. The ke
queenTau
NORMAL
2021-08-07T01:40:20.002017+00:00
2021-08-07T19:11:12.392861+00:00
899
false
So the challenge here is how can we quickly sum the k-largest and k-smallest elements, and I want to talk about how I did that with a binary search tree. The key idea was to store the number of items in each sub-tree, as well as the sum of all the items in each sub tree. By storing the size of each node it becomes poss...
10
0
['Binary Search Tree', 'Python']
2
finding-mk-average
Python [short solution]
python-short-solution-by-gsan-5g05
Warning: As the wonderful test cases have been updated, now the code gets TLE.\n\nThis one is slightly outlandish as I use the SortedList from sortedcontainers
gsan
NORMAL
2021-04-11T04:01:35.464062+00:00
2021-04-15T09:23:08.974367+00:00
1,090
false
**Warning: As the wonderful test cases have been updated, now the code gets TLE.**\n\nThis one is slightly outlandish as I use the `SortedList` from `sortedcontainers` class - it\'s not part of standard library. With that said this container proves useful in several questions, such as "1649. Create Sorted Array through...
10
1
[]
3
finding-mk-average
C++ 2 methods (Segment Tree && heaps)
c-2-methods-segment-tree-heaps-by-tom072-d0cp
The core of this problem is:\n\n> How to find the sum of the smallest/biggest 1~k th numbers in an array?\n\nSegment tree can do this in O(nlogn) time.\n\nWe ca
tom0727
NORMAL
2021-04-11T06:08:34.454442+00:00
2021-04-11T13:19:03.264614+00:00
823
false
The core of this problem is:\n\n> How to find the sum of the smallest/biggest 1~k th numbers in an array?\n\nSegment tree can do this in `O(nlogn)` time.\n\nWe can keep a segment tree which stores the information about: "For a given value x, how many x are there in the array?".\n\nFor example the current array is `[3,1...
9
1
[]
0
finding-mk-average
[Java] Elegant O(log M)-add O(1)-avg TreeSet Solution
java-elegant-olog-m-add-o1-avg-treeset-s-1f02
Thanks to the suggestion by @anindya-saha; Here is a very elegant Solution. The solution works great in these questions: \n 295. Find Median from Data Stream\n
xieyun95
NORMAL
2021-05-22T06:57:24.519303+00:00
2021-06-02T04:27:57.662087+00:00
1,006
false
Thanks to the suggestion by @anindya-saha; Here is a very elegant Solution. The solution works great in these questions: \n* [295. Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/discuss/1246287/Java-clean-O(logN)-TreeSet-Solution-oror-with-comments)\n* [1825. Finding MK Average...
8
0
['Tree', 'Ordered Set', 'Java']
4
finding-mk-average
Simple 97.11% O(n log n) Python3 solution with queue, map and two pointers
simple-9711-on-log-n-python3-solution-wi-vp11
Hey guys, this is my first time posting here and english is not my first language, hopefully i can be clear but i think the code is simple enough\n \n\n# Intuit
user8889zs
NORMAL
2024-04-30T02:16:09.100674+00:00
2024-04-30T02:16:09.100693+00:00
806
false
Hey guys, this is my first time posting here and english is not my first language, hopefully i can be clear but i think the code is simple enough\n \n![Sem t\xEDtulo.png](https://assets.leetcode.com/users/images/423b1896-2b52-420d-b72f-f6a4f409b58d_1714441493.5260954.png)\n# Intuition\nAt first it did it the brute forc...
7
0
['Python3']
1
finding-mk-average
C++ Easy Deque + Ordered Map [Add O(logM) Avg O(K) Space O(M)]
c-easy-deque-ordered-map-add-ologm-avg-o-mcud
Idea\n##### addElement\n- Use a deque to keep track of the last m elements.\n- Use a ordered map to store the m elements in order.\n- Use a varaiable sum to sum
carbonk
NORMAL
2021-05-19T07:54:58.633229+00:00
2021-05-20T01:47:52.892989+00:00
350
false
### Idea\n##### addElement\n- Use a deque to keep track of the last m elements.\n- Use a ordered map to store the m elements in order.\n- Use a varaiable sum to sum all m elements\n\n#### calculateMKAverage\n- We iterate through the first K and last K elements in ordered map, and subtract them from sum. And calculate t...
7
0
[]
2
finding-mk-average
Binary Indexed Trees (BIT or Fenwick tree) + Binary lifting (logN Time Complexity)
binary-indexed-trees-bit-or-fenwick-tree-xjnk
ref: https://leetcode-cn.com/circle/article/OeMXPy/\nTwo properties of BIT:\n(1) nodes[i] or sums[i] manages the sum in the range nums[i - i & (-i), i - 1].\ne.
longhun
NORMAL
2021-04-14T09:06:24.895436+00:00
2021-10-04T00:00:48.357012+00:00
1,045
false
ref: https://leetcode-cn.com/circle/article/OeMXPy/\nTwo properties of BIT:\n(1) nodes[i] or sums[i] manages the sum in the range nums[i - i & (-i), i - 1].\ne.g.\nnodes[4] = nums[0] + nums[1] + nums[2] + nums[3]\nnodes[6] = nums[4] + nums[5]\n(2) parent(i) = i + i&(-i)\n\nAssuming we have added nums[0], ..., nums[7] t...
7
0
[]
2
finding-mk-average
Don't use SortedList in interviews. Three MinMaxHeap. Add: O(logM + log(M-2K)), Calc:O(1). Beats 96%
dont-use-sortedlist-in-interviews-three-9woll
Intuition\n Describe your first thoughts on how to solve this problem. \nIn the inteview, usually we cannot use SortedList which is not in standard libraries. D
four_points
NORMAL
2024-05-15T16:47:00.906908+00:00
2024-05-15T16:53:10.453506+00:00
1,387
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the inteview, usually we cannot use SortedList which is not in standard libraries. Doublecheck your goal, passing interviews or just solving leetcode?\n\n# Approach\nSplit data into 3 parts. And make each part as MinMaxHeap so that we ...
6
0
['Heap (Priority Queue)', 'Python3']
5
finding-mk-average
Java TreeSet | Easy to Understand
java-treeset-easy-to-understand-by-mayan-18ax
\nclass MKAverage {\n TreeSet<Long> small, medium, large;\n Queue<Long> queue;\n long sum = 0;\n int m, k, total;\n long ind = 0;\n public MKA
mayank12559
NORMAL
2021-04-11T05:04:25.556288+00:00
2021-04-11T07:19:59.021625+00:00
883
false
```\nclass MKAverage {\n TreeSet<Long> small, medium, large;\n Queue<Long> queue;\n long sum = 0;\n int m, k, total;\n long ind = 0;\n public MKAverage(int m, int k) {\n queue = new ArrayDeque();\n small = new TreeSet();\n large = new TreeSet();\n medium = new TreeSet();\n ...
6
0
[]
1
finding-mk-average
Simple and easy to understand
simple-and-easy-to-understand-by-sachint-4om0
Intuition\n1. use List to maintain the order\n2. Use Treemap to maintain the sorted order \n3. In add method check the size and take action accordingly \n4. In
sachintech
NORMAL
2024-03-08T18:43:40.452320+00:00
2024-03-08T18:43:40.452341+00:00
389
false
# Intuition\n1. use List to maintain the order\n2. Use Treemap to maintain the sorted order \n3. In add method check the size and take action accordingly \n4. In calculate method skip first k elament and last k element \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time c...
5
0
['Java']
1
finding-mk-average
Python3 Keeping Three Containers For Small/Middle/Large Values (Runtime 98%, Memory 94%)
python3-keeping-three-containers-for-sma-fdoq
Intuition\nSince this data structure deals only with the last m elements from a data stream, it\'s natural to use a deque that only stores the latest m elements
hoyyang
NORMAL
2023-08-20T20:39:24.189812+00:00
2023-08-20T20:39:24.189837+00:00
663
false
# Intuition\nSince this data structure deals only with the last m elements from a data stream, it\'s natural to use a deque that only stores the latest m elements (*self.container*). The first solution I came up was precisely that, as can be seen from the commented data block. \n\nIn that straight-forward approach, whe...
5
0
['Python3']
1
finding-mk-average
[Python3] 1-SortedList + Queue with a thought process.
python3-1-sortedlist-queue-with-a-though-2zam
Intuition\n To manage current m values, we can use a queue.\n But if we sort and count the middle sum on every insertion, A single Insertion will take O(MlogM)
asayu123
NORMAL
2022-11-08T06:14:45.990291+00:00
2022-11-08T06:32:46.861819+00:00
504
false
**Intuition**\n* To manage current m values, we can use a queue.\n* But if we sort and count the middle sum on every insertion, A single Insertion will take O(MlogM) time, O(TM log M) as total where M is the number of elements in the window, T is the total stream elements. \n\n* So we can use the following ideas.\n\t*...
5
0
['Queue', 'Python3']
0
finding-mk-average
Python3 simple solution
python3-simple-solution-by-mnikitin-uebx
\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.values = [0] * m\n self.m = m\n self.k = k\n self.idx = 0\n\n
mnikitin
NORMAL
2021-04-11T04:01:23.443245+00:00
2021-04-11T04:04:48.105554+00:00
397
false
```\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.values = [0] * m\n self.m = m\n self.k = k\n self.idx = 0\n\n def addElement(self, num: int) -> None:\n self.values[self.idx % self.m] = num\n self.idx += 1\n\n def calculateMKAverage(self) -> int:\n ...
5
5
[]
5
finding-mk-average
Easy to Understand With Examples using Three TreeSet
easy-to-understand-with-examples-using-t-vwob
Intuition\n\nLet\'s walk through the intuition behind this solution with a concrete example. Suppose we\'re given m = 5 and k = 1, and we start receiving a stre
najimali
NORMAL
2024-04-08T15:50:08.992380+00:00
2024-04-08T15:50:08.992408+00:00
405
false
# Intuition\n\nLet\'s walk through the intuition behind this solution with a concrete example. Suppose we\'re given `m = 5` and `k = 1`, and we start receiving a stream of integers.\n\n### Initial Stream: `[3, 1, 10, 7, 2]`\n\n- Our objective is to calculate the MKAverage for the last `m = 5` elements in the stream, af...
4
0
['Java']
0
finding-mk-average
Solution using BST. Easy to understand
solution-using-bst-easy-to-understand-by-6vlp
Intuition\nUse BST as we are keeping track of sorted items. \n\n# Approach\nThere can be multiple approaches to solve this problem. Let me describe three approa
patel_manish
NORMAL
2023-12-04T10:09:03.532851+00:00
2023-12-04T10:09:03.532885+00:00
405
false
# Intuition\nUse BST as we are keeping track of sorted items. \n\n# Approach\nThere can be multiple approaches to solve this problem. Let me describe three approaches (note that first two approaches lead to TLE here):\n\n1. Brute force: We can do exactly as the problem says. Maintain a list of all elements. When an add...
4
0
['Python3']
1
finding-mk-average
My Intuitive C++ Solution, to solve most of streaming data Problem
my-intuitive-c-solution-to-solve-most-of-v7m1
Intuition\n Describe your first thoughts on how to solve this problem. \nIf you have solved other streaming data related problem. You can build this solution ba
khalid007
NORMAL
2023-09-02T13:41:14.839036+00:00
2023-09-02T14:18:51.522108+00:00
636
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf you have solved other streaming data related problem. You can build this solution based on the solution of other similar problems. One such problem is median in a streaming data. \n\n# Approach\nFor this you have to keep in mind few th...
4
0
['Queue', 'Heap (Priority Queue)', 'Data Stream', 'Ordered Set', 'C++']
0
finding-mk-average
JAVA EASY SOLUTION || TREEMAP || QUEUE
java-easy-solution-treemap-queue-by-anik-td25
\nclass MKAverage {\n\n int sum,total,m,k;\n\n TreeMap<Integer,Integer> map=new TreeMap<>();\n Queue<Integer> queue=new LinkedList<>();\n public MKAve
aniket7419
NORMAL
2022-02-03T07:59:52.873856+00:00
2022-02-03T07:59:52.873901+00:00
302
false
```\nclass MKAverage {\n\n int sum,total,m,k;\n\n TreeMap<Integer,Integer> map=new TreeMap<>();\n Queue<Integer> queue=new LinkedList<>();\n public MKAverage(int m, int k) {\n\n this.m=m;\n this.k=k;\n }\n\n public void addElement(int num) {\n\n total++;\n sum+=num;\n queue...
4
0
[]
1
finding-mk-average
Clean Solution using three TreeMaps | Easy to understand
clean-solution-using-three-treemaps-easy-so9h
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
kesarwaniankit4812
NORMAL
2024-04-15T19:26:12.550348+00:00
2024-04-15T19:26:12.550404+00:00
842
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n$$O(log(n))$$ for *addElement*\n$$O(1)$$ for *calculateMKAverage*\n\n- Space complexity:\n<!-- Add your space complexity here, e.g....
3
0
['Java']
0
finding-mk-average
C++ Solution Using Only 1 Multiset
c-solution-using-only-1-multiset-by-sequ-i9my
Intuition\nmost solutions use 3 multisets, but actually 1 is enough.\n\nBasically we need a queue of size m to support the sliding window. We also want the elem
sequoia18
NORMAL
2024-01-14T21:34:24.375248+00:00
2024-08-11T12:42:37.803466+00:00
445
false
# Intuition\nmost solutions use 3 multisets, but actually 1 is enough.\n\nBasically we need a queue of size `m` to support the sliding window. We also want the elements inside the window to be sorted so that we can get the middle elements. To support this, We use another multiset (C++ STL\'s implementation of binary se...
3
0
['C++']
2
finding-mk-average
[C++] Modular Code With 3 Maps | O(Log(N)) Time
c-modular-code-with-3-maps-ologn-time-by-zsyv
\n/* \n Time: addElement: O(logm) | calculateMKAverage: O(1)\n Space: O(m)\n Tag: TreeMap, Sorting, Queue\n Difficulty: H\n*/\n\nclass MKAverage {\n
meKabhi
NORMAL
2022-07-06T22:42:00.887535+00:00
2022-07-06T22:42:00.887563+00:00
871
false
```\n/* \n Time: addElement: O(logm) | calculateMKAverage: O(1)\n Space: O(m)\n Tag: TreeMap, Sorting, Queue\n Difficulty: H\n*/\n\nclass MKAverage {\n map<int, int> left, middle, right;\n queue<int> q;\n int sizeofLeft, sizeofMiddle, sizeofRight;\n int k;\n long long mkSum;\n int m;\n\n ...
3
0
['Tree', 'Queue', 'C', 'Sorting', 'C++']
1
finding-mk-average
c++(404ms 78%) queue and set
c404ms-78-queue-and-set-by-zx007pi-fg9w
Runtime: 404 ms, faster than 78.26% of C++ online submissions for Finding MK Average.\nMemory Usage: 146 MB, less than 62.50% of C++ online submissions for Find
zx007pi
NORMAL
2021-09-24T06:25:17.468251+00:00
2021-09-24T06:43:45.194789+00:00
672
false
Runtime: 404 ms, faster than 78.26% of C++ online submissions for Finding MK Average.\nMemory Usage: 146 MB, less than 62.50% of C++ online submissions for Finding MK Average.\n**General idea :**\n**1.** create queue with current of elements {element, id of element in stream} \n**2.** while size of queue less than **m*...
3
0
['C', 'C++']
0
finding-mk-average
Java | Just one TreeSet | add O(logM) avg O(1)
java-just-one-treeset-add-ologm-avg-o1-b-ihak
Last m stream elements are kept sorted using TreeSet. First K and Last K elements are identified by remembering the edge elements, and updated with each additio
prezes
NORMAL
2021-05-19T21:46:23.236922+00:00
2021-05-19T22:14:30.095741+00:00
287
false
Last m stream elements are kept sorted using TreeSet. First K and Last K elements are identified by remembering the edge elements, and updated with each addition (with O(logM)). Sum of m-2k "middle" elements is updated with each addition (and used to calculate average in O(1)). \nDuplicate elements are distinguished us...
3
0
[]
0
finding-mk-average
Java O(1) Calcualte, O(log(m)) add. Three MultiSet
java-o1-calcualte-ologm-add-three-multis-f1f7
Should be short if Java has a native MultiSet implement as Guava TreeMultiSet\n\nclass MKAverage {\n private MultiTreeSet bottom = new MultiTreeSet();\n p
janevans
NORMAL
2021-04-11T04:27:44.097496+00:00
2021-04-11T04:27:44.097521+00:00
266
false
Should be short if Java has a native MultiSet implement as [Guava TreeMultiSet](https://guava.dev/releases/21.0/api/docs/com/google/common/collect/TreeMultiset.html)\n```\nclass MKAverage {\n private MultiTreeSet bottom = new MultiTreeSet();\n private MultiTreeSet middle = new MultiTreeSet();\n private MultiTr...
3
1
[]
1
finding-mk-average
Queue + Multiset | C++ | Simple Solution
queue-multiset-c-simple-solution-by-void-np39
\nclass MKAverage {\npublic:\n \n queue<int> q;\n multiset<int> mk;\n int total;\n int kt;\n MKAverage(int m, int k) {\n total = m;\n
voidp
NORMAL
2021-04-11T04:05:04.830718+00:00
2021-04-11T04:05:04.830743+00:00
197
false
```\nclass MKAverage {\npublic:\n \n queue<int> q;\n multiset<int> mk;\n int total;\n int kt;\n MKAverage(int m, int k) {\n total = m;\n kt = k;\n }\n \n void addElement(int num) {\n \n if(q.size() == total){\n int e = q.front();\n q.pop();\n ...
3
4
[]
1
finding-mk-average
C++ | Multiset Solution
c-multiset-solution-by-kena7-4h3f
Code
kenA7
NORMAL
2025-04-05T07:12:45.795287+00:00
2025-04-05T07:12:45.795287+00:00
51
false
# Code ```cpp [] #include <bits/stdc++.h> using namespace std; class MKAverage { public: multiset<int> left, right, mid; deque<int> dq; long m, k, sum, midSize; MKAverage(int m, int k) { this->m = m; this->k = k; sum = 0; midSize = m - 2 * k; } void remove(int...
2
0
['C++']
0
finding-mk-average
Using min-max heaps: beats 90% time / 100% memory
using-min-max-heaps-beats-90-time-100-me-rit9
Intuition\nThe goal hele is to prvide a way of querying for some statistics over the sorted order of a dynamic set of elements - the previous m values at the mo
pefreis
NORMAL
2024-06-07T12:46:25.764886+00:00
2024-06-07T21:47:45.672610+00:00
440
false
# Intuition\nThe goal hele is to prvide a way of querying for some statistics over the sorted order of a dynamic set of elements - the previous $$m$$ values at the moment we are queried. Obviously, this can be accomplished by sorting the set of previous $$m$$ items each time we are queried, but in order to be efficient...
2
0
['Queue', 'Heap (Priority Queue)', 'Data Stream', 'Go']
0
finding-mk-average
3 Sorted collections approach in C#
3-sorted-collections-approach-in-c-by-ar-3ccp
Intuition\n\nWe need to maintain 3 sorted sequences. \n\nThe tricky part is, there is no good way to represent sorted Sequences in C#, so we will create one.\n\
artbidn
NORMAL
2024-03-24T02:07:04.908062+00:00
2024-03-24T02:07:04.908099+00:00
212
false
# Intuition\n\nWe need to maintain 3 sorted sequences. \n\nThe tricky part is, there is no good way to represent sorted Sequences in C#, so we will create one.\n\n# Approach\n\nOne thing should be obvious. At first, we are just adding elements to a sequence, but eventually we first need to \'pop\' the oldest adding ele...
2
0
['C#']
1
finding-mk-average
Multiset | Java | O(NlogM) time | O(M) space
multiset-java-onlogm-time-om-space-by-go-i19x
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nUsed three MultiSet to track the state of the elements added.\nsmallest -
govindarajss
NORMAL
2024-03-11T03:28:17.850925+00:00
2024-03-11T03:28:17.850970+00:00
377
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nUsed three MultiSet to track the state of the elements added.\nsmallest - tracks the smallest k elemtns in the current window\nlargest - tracks the smallest k elemtns in the current window\nothers - tracks the rest of the el...
2
0
['Java']
0
finding-mk-average
Python SortedList
python-sortedlist-by-nblam1994-3lli
Complexity\n- Time complexity: O(NlogN)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(N)\n Add your space complexity here, e.g. O(n) \n\n
nblam1994
NORMAL
2024-01-14T04:57:26.538704+00:00
2024-01-14T04:57:26.538735+00:00
388
false
# Complexity\n- Time complexity: O(NlogN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom sortedcontainers import SortedList\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.queue = ...
2
0
['Python3']
0
finding-mk-average
Easy to Understand 3 Multisets Approach
easy-to-understand-3-multisets-approach-xcqw8
Approach:\nIn this problem we will be using SlidingWindow and Multiset so solve this problem.\nFor SlidingWindow we are using a queue stream and 3 Multisets kBo
badhansen
NORMAL
2024-01-01T23:15:59.675137+00:00
2024-01-01T23:15:59.675161+00:00
871
false
### Approach:\nIn this problem we will be using **SlidingWindow** and **Multiset** so solve this problem.\nFor **SlidingWindow** we are using a `queue` `stream` and 3 **Multisets** `kBottom`, `middle` and `kTop` track the three buckets:\n\n**We will divide the 3 bucket as:**\n1. Bottom `K` numbers, MIDDLE numbers `(M -...
2
0
['Queue', 'C', 'C++']
0
finding-mk-average
Python, Binary search and double-ended queue. Showing clear thread of thoughts
python-binary-search-and-double-ended-qu-xqya
Intuition\n\nThought 1: We start from the naive solution\n Idea: Keep a container of m elements (data structure: double-ended queue, probably a doubly-linked li
qihangyao
NORMAL
2023-11-10T17:35:20.614836+00:00
2023-11-10T17:35:20.614857+00:00
67
false
# Intuition\n\nThought 1: We start from the naive solution\n* Idea: Keep a container of m elements (data structure: double-ended queue, probably a doubly-linked list in exact implementation)\n* AddElement -> cost O(1)\n* calculateMKAverage -> sort (O(m log m)))\n\nThought 2: Can we do better for calculate MKAverage?\n*...
2
0
['Python3']
0
finding-mk-average
c++ solution
c-solution-by-dilipsuthar60-r3o3
\nclass MKAverage\n{\n public:\n multiset<int> left, mid, right;\n int k;\n int m;\n long long sum;\n queue<int> q;\n MKAverage(int mt,
dilipsuthar17
NORMAL
2022-06-19T18:10:31.830632+00:00
2022-06-19T18:10:31.830679+00:00
689
false
```\nclass MKAverage\n{\n public:\n multiset<int> left, mid, right;\n int k;\n int m;\n long long sum;\n queue<int> q;\n MKAverage(int mt, int kt)\n {\n sum = 0;\n m = mt;\n k = kt;\n }\n void remove()\n {\n int val = q.front();\n q.pop();\n ...
2
0
['C', 'C++']
0
finding-mk-average
[C++] simple solution using sorted map faster than 98%
c-simple-solution-using-sorted-map-faste-jm73
\nclass MKAverage {\npublic:\n vector<int> items;\n map<int,int> lastM;\n int m,k,s,e;\n long total;\n \n MKAverage(int m, int k) {\n t
ltbtb_rise
NORMAL
2021-09-22T13:06:10.925831+00:00
2021-09-22T13:21:53.536565+00:00
309
false
```\nclass MKAverage {\npublic:\n vector<int> items;\n map<int,int> lastM;\n int m,k,s,e;\n long total;\n \n MKAverage(int m, int k) {\n this->m = m;\n this->k = k;\n s = 0;\n e = 0;\n total = 0;\n }\n \n\t// O(logM)\n void addElement(int num) {\n ite...
2
1
[]
1
finding-mk-average
What should I do when meeting this question in interview - A step by step guide
what-should-i-do-when-meeting-this-quest-5twc
Code first.\n\nclass MultiMap {\n public TreeMap<Integer, Integer> map;\n private int cnt = 0;\n private double sum = 0.0;\n \n public MultiMap()
hammer001
NORMAL
2021-08-22T06:43:28.586133+00:00
2021-08-22T06:43:28.586163+00:00
260
false
Code first.\n```\nclass MultiMap {\n public TreeMap<Integer, Integer> map;\n private int cnt = 0;\n private double sum = 0.0;\n \n public MultiMap() {\n this.map = new TreeMap<>();\n }\n \n public void add(Integer key) {\n this.cnt ++;\n this.sum += key;\n this.map.pu...
2
1
[]
1
finding-mk-average
C++ TreeMap+Queue
c-treemapqueue-by-verdict_ac-qfgn
\nclass MKAverage {\npublic:\n map<int,int>record;\n long long total;\n queue<int>q;\n int m;\n int k;\n MKAverage(int m, int k) {\n th
Verdict_AC
NORMAL
2021-06-06T13:44:09.819221+00:00
2021-06-06T13:44:09.819266+00:00
243
false
```\nclass MKAverage {\npublic:\n map<int,int>record;\n long long total;\n queue<int>q;\n int m;\n int k;\n MKAverage(int m, int k) {\n this->m=m;\n this->k=k;\n total=0;\n }\n \n void addElement(int num) {\n q.push(num);\n total+=(long long)num;\n re...
2
0
['Tree', 'Queue']
0
finding-mk-average
[Java] 3 TreeSets + 1 Queue, clear and easy-to-write code
java-3-treesets-1-queue-clear-and-easy-t-nttj
Similar problems\nThis problem is a variant of\n- 295. Find Median from Data Stream\n- 480. Sliding Window Median\n\nAnd I strongly suggest you read this post,
maristie
NORMAL
2021-06-05T03:33:38.617653+00:00
2021-06-05T03:43:48.602190+00:00
198
false
## Similar problems\nThis problem is a variant of\n- [295. Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)\n- [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)\n\nAnd I strongly suggest you read [this post](https://leetcode.com/problems/sliding...
2
0
[]
0
finding-mk-average
C++ Intuitive Fenwick Tree Solution O(N * (logN)^2) w/explanation
c-intuitive-fenwick-tree-solution-on-log-00hd
First of all we only need to maintain a windows of size m at any point, so we maintain a queue for storing the numbers. \n\nFor MK Avg calculation, the whole se
varkey98
NORMAL
2021-04-14T18:05:57.380014+00:00
2021-04-14T18:07:47.632042+00:00
363
false
First of all we only need to maintain a windows of size m at any point, so we maintain a queue for storing the numbers. \n\nFor MK Avg calculation, the whole search space of ```[1,10^5]``` is used for each testcase. \nI used 2 Fenwick Trees, for finding the sum of a given range in ```O(logN)``` time, the first tree is ...
2
0
['C', 'Binary Tree']
2
finding-mk-average
Java BIT/Fenwick 60ms
java-bitfenwick-60ms-by-zoro406-zpzs
\nclass MKAverage {\n Deque<Integer> queue;\n Bit countBit;\n Bit valueBit;\n int m;\n int k;\n public MKAverage(int m, int k) {\n queu
zoro406
NORMAL
2021-04-13T07:47:01.072246+00:00
2021-04-13T07:50:53.811197+00:00
316
false
```\nclass MKAverage {\n Deque<Integer> queue;\n Bit countBit;\n Bit valueBit;\n int m;\n int k;\n public MKAverage(int m, int k) {\n queue = new ArrayDeque();\n countBit = new Bit(100001);\n valueBit = new Bit(100001);\n this.m = m;\n this.k = k;\n }\n \n p...
2
0
[]
0
finding-mk-average
Naive Java solution with 2 PQs + TreeMap, probably will be TLE soon though
naive-java-solution-with-2-pqs-treemap-p-ctfj
Comments are in line.\n\nI expect this to be TLE soon with more test cases added, but I find this to be naive enough so sharing it.\n\n\nclass MKAverage {\n
kwi_t
NORMAL
2021-04-11T05:00:29.811527+00:00
2021-04-11T05:04:27.378976+00:00
212
false
Comments are in line.\n\nI expect this to be TLE soon with more test cases added, but I find this to be naive enough so sharing it.\n\n```\nclass MKAverage {\n int m;\n int k;\n List<Integer> lastM; // Record last m numbers, ordered by when they were added. This is for removing number.\n TreeMap<Integer, In...
2
0
[]
1
finding-mk-average
[C++]Fenwick Tree Solution.
cfenwick-tree-solution-by-waluigi120-0zmk
The idea for the problem is to maintain a data structure so that it can list the first k numbers\' sum and last k numbers\' sum quickly. It will also have the c
waluigi120
NORMAL
2021-04-11T04:06:29.673018+00:00
2021-04-11T04:06:29.673055+00:00
380
false
The idea for the problem is to maintain a data structure so that it can list the first k numbers\' sum and last k numbers\' sum quickly. It will also have the capability of counting how many elements there are between specific range of the values. The data structure has the aformentioned abilities is a Fenwick Tree. Yo...
2
0
[]
0
finding-mk-average
Multiset | Queue O(logn) Time complexity
multiset-queue-ologn-time-complexity-by-adxtx
Intuition\n- Try to visualize it what are our requirements.\n- Firstly we need to maintain a window of size m so that only latest m elements are considered.\n-
yash559
NORMAL
2024-10-06T19:31:14.268419+00:00
2024-10-06T19:31:14.268441+00:00
113
false
# Intuition\n- Try to visualize it what are our requirements.\n- Firstly we need to maintain a **window of size m** so that only latest m elements are considered.\n- Which data structure can we think of? yes it\'s **queue** when size exceeds m just pop oldest value and insert latest value.\n- Now our main aim is to act...
1
0
['C++']
0
finding-mk-average
Java Queue & TreeMap Solution
java-queue-treemap-solution-by-fireonser-mjkv
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need last m elements -> we use queue to record last m elements\nWe need to remove k
fireonserver
NORMAL
2024-08-22T16:22:49.162137+00:00
2024-08-22T16:22:49.162166+00:00
43
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need last m elements -> we use queue to record last m elements\nWe need to remove k smallest and k largest\n1. total number of elements to be added is m - 2 * k\n2. Use TreeMap to remove k smallest\n\n# Approach\n<!-- Describe your app...
1
0
['Java']
1
finding-mk-average
C++| Multisets
c-multisets-by-chandrasekhar_n-6inm
Intuition\nUse a Queue to strore elements from the Q, max size of the Q should be m. Use 3 multisets, 1 for smallest K elements, second set for for M-2K mid ele
Chandrasekhar_N
NORMAL
2024-08-14T12:25:03.228207+00:00
2024-08-14T12:25:03.228236+00:00
83
false
# Intuition\nUse a Queue to strore elements from the Q, max size of the Q should be m. Use 3 multisets, 1 for smallest K elements, second set for for M-2K mid elements, third set for largest K elements. For every element added, pop Q front if its size > m and then update the sets and sum \n\n# Approach\n<!-- Describe y...
1
0
['C++']
1
finding-mk-average
Using multiset iterators
using-multiset-iterators-by-miguel-l-ata9
Intuition\n Describe your first thoughts on how to solve this problem. \nKeep elements in a sorted container to easily exclude the k lowest and highest by keepi
miguel-l
NORMAL
2024-07-13T17:34:33.676905+00:00
2024-07-13T17:34:33.676940+00:00
73
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nKeep elements in a sorted container to easily exclude the k lowest and highest by keeping track of the sum. Use a queue and some pointers to keep the container updated.\n\n# Approach\n<!-- Describe your approach to solving the problem. --...
1
0
['C++']
0
finding-mk-average
C++ Simple *ONE* Map & deque
c-simple-one-map-deque-by-vishnum1998-xmr9
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUse one map which store
vishnum1998
NORMAL
2024-07-03T16:12:58.359652+00:00
2024-07-03T16:12:58.359698+00:00
85
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse one map which stores the key and count. It auoto sorts the data.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\ncalculat...
1
0
['C++']
0
finding-mk-average
Java, Design a class to store sorted nums with duplicates
java-design-a-class-to-store-sorted-nums-x4ic
Intuition\nIf we implement a brute force solution, we will make the following observations:\n1. We need to store only m recent numbers of stream\n2. If the numb
Vladislav-Sidorovich
NORMAL
2024-06-13T10:51:44.608319+00:00
2024-06-13T10:51:44.608338+00:00
40
false
# Intuition\nIf we implement a brute force solution, we will make the following observations:\n1. We need to store only `m` recent numbers of stream\n2. If the numbers will be sorted we in consant time may remove `k` smalles/biggest numbers\n3. We may keep track of `sum` of `[k+1, m-k-2]` sorted numbers\n\nTaking into ...
1
0
['Queue', 'Ordered Map', 'Java']
0
finding-mk-average
heap | bst
heap-bst-by-hzhaoc-csbm
heap with lazy update 700ms\n\npython\nclass MKAverage:\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.lh1, self
hzhaoc
NORMAL
2024-01-31T04:21:41.655027+00:00
2024-01-31T04:21:41.655055+00:00
196
false
# heap with lazy update 700ms\n\n```python\nclass MKAverage:\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.lh1, self.rh1 = [(0, i) for i in range(m-k)], [(0, i) for i in range(m-k, m)]\n self.lh2, self.rh2 = [(0, i) for i in range(k)], [(0, i) for i in range(k, m)...
1
0
['Binary Search Tree', 'Heap (Priority Queue)', 'Python3']
0
finding-mk-average
3 TreeMaps (Clean Code)
3-treemaps-clean-code-by-khamidjon-n0xi
Intuition\nThe idea is to have 3 treemaps tl, tm and th. For lowest k (tl), highest k (th) and all others (tm). The trick is we need to balance the maps when an
khamidjon
NORMAL
2023-12-29T23:38:37.234088+00:00
2023-12-29T23:39:29.764893+00:00
142
false
# Intuition\nThe idea is to have 3 treemaps **tl**, **tm** and **th**. For lowest k (tl), highest k (th) and all others (tm). The trick is we need to balance the maps when an element is added/removed. \n\n\n# Code\n```\nclass MKAverage {\n int mx = (int)2e5, mn = 0, i = 0, m, k;\n long sm = 0l;\n TreeMap<Integ...
1
0
['Java']
0
finding-mk-average
3 multiset and a queue
3-multiset-and-a-queue-by-pranavgaur2000-mcxc
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
pranavgaur2000
NORMAL
2023-12-23T16:37:47.513188+00:00
2023-12-23T16:37:47.513208+00:00
450
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n For average: O(1)\n For insert: O(log(m))\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m)...
1
0
['C++']
0
finding-mk-average
A sub optimal solution which is possible to code fully in an interview
a-sub-optimal-solution-which-is-possible-i02y
Intuition\n Describe your first thoughts on how to solve this problem. \nWhile solving the problem I got the intuition to maintain three heaps, or dividing an a
user7030K
NORMAL
2023-11-24T15:57:53.793234+00:00
2023-11-24T15:57:53.793268+00:00
330
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhile solving the problem I got the intuition to maintain three heaps, or dividing an array to three parts and keeping sum of these tree parts and update if any element is added or deleted from each. But I was finding it very difficult to...
1
0
['Doubly-Linked List', 'Python']
1
finding-mk-average
Straight forward solution
straight-forward-solution-by-ogoyal-hbzn
Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque
ogoyal
NORMAL
2023-11-06T00:53:55.739611+00:00
2023-11-06T00:53:55.739632+00:00
38
false
# Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque is suitable for the rolling window, and a Counter can efficiently track number frequencies.\n\n# Approach\n1. Initialization: Use a deque to maintain the last...
1
0
['Python3']
0
finding-mk-average
Best Java Solution || Beats 100%
best-java-solution-beats-100-by-ravikuma-5tpr
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ravikumar50
NORMAL
2023-10-08T19:22:46.478973+00:00
2023-10-08T19:22:46.478991+00:00
591
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
2
finding-mk-average
Python SortedList - intuitive logic and explanation
python-sortedlist-intuitive-logic-and-ex-gntm
Algorithm:\n1. Maintaina queue for keeping items in stream order and 3 sorted lists - lesser, mid and greater for keeping track of the k lesser items, k greater
subhashgo
NORMAL
2023-10-05T01:28:12.571050+00:00
2023-10-05T01:28:12.571078+00:00
39
false
Algorithm:\n1. Maintaina queue for keeping items in stream order and 3 sorted lists - lesser, mid and greater for keeping track of the k lesser items, k greater items and m-2k selected items that need to be averaged\n2. When the stream size exceeds m, we need to remove the oldest entry from the stream i.e. the first en...
1
0
['Python', 'Python3']
0
finding-mk-average
Python3 1 SortedList solution & C++ 3 set solution
python3-1-sortedlist-solution-c-3-set-so-b5ua
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nThis is a very nice problem to test your ability to analyze what data structure is
huikinglam02
NORMAL
2023-07-25T06:25:38.081260+00:00
2023-07-26T02:07:08.212241+00:00
37
false
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThis is a very nice problem to test your ability to analyze what data structure is needed, how to design a working system, what data structure different languages provides and their limitations.\r\n# Approach\r\n<!-- Describe your app...
1
0
['C++', 'Python3']
0
finding-mk-average
Python (Simple Deque + SortedList)
python-simple-deque-sortedlist-by-rnotap-65xy
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
rnotappl
NORMAL
2023-04-23T17:32:40.344789+00:00
2023-04-23T17:32:40.344822+00:00
65
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Python3']
1
finding-mk-average
C# Ordered Set + Queue
c-ordered-set-queue-by-justunderdog-ypkl
Complexity\n- Time complexity:\n - MKAverage(int m, int k): O(1)\n\n - AddElement(int num):\n - count < m: O(1)\n - count == m: O(k + mlogm)
justunderdog
NORMAL
2022-11-19T16:19:58.370762+00:00
2022-11-19T16:19:58.370791+00:00
125
false
# Complexity\n- Time complexity:\n - MKAverage(int m, int k): $$O(1)$$\n\n - AddElement(int num):\n - count < m: $$O(1)$$\n - count == m: $$O(k + mlogm)$$\n - count > m: $$O(logm)$$\n - CalculateMKAverage(): $$O(1)$$\n\n- Space complexity: $$O(m)$$\n\n# Code\n```\npublic class MKAverage\n{...
1
0
['Queue', 'Ordered Set', 'C#']
1
finding-mk-average
Python: Faster than 100% using 1 hash
python-faster-than-100-using-1-hash-by-t-psyk
Algorithm is fairly straightforward. Keep track of the m window with a hash. Use hash to find MK Average. Ran 4x. Faster than 100% each time and 100% less memor
trolleysunrise
NORMAL
2022-08-31T02:45:28.208125+00:00
2022-08-31T02:46:48.046031+00:00
169
false
Algorithm is fairly straightforward. Keep track of the m window with a hash. Use hash to find MK Average. Ran 4x. Faster than 100% each time and 100% less memory 3x (89% the other).\n\nMore Details:\n\nOn insert\n1. Increment the counter for the newest number in the hash.\n2. If we\'ve already added m elements to the h...
1
0
['Python']
0
finding-mk-average
Python Solution SortedList
python-solution-sortedlist-by-user6397p-wxny
\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.arr = SortedList()\n self.m = m\n
user6397p
NORMAL
2022-08-14T15:09:15.669749+00:00
2022-08-14T15:09:15.669791+00:00
358
false
```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.arr = SortedList()\n self.m = m\n self.k = k\n self.q = deque()\n self.total = None\n\n def addElement(self, num: int) -> None:\n self.q.append(num)\n m ...
1
0
['Binary Search Tree', 'Python3']
0
finding-mk-average
Segment Tree Solution
segment-tree-solution-by-i_see_you-wsqs
\nclass MKAverage {\npublic:\n #define LL long long\n struct node {\n int frq;\n LL sum;\n \n node() { frq = 0; sum = 0; }\n
i_see_you
NORMAL
2022-05-20T23:31:29.837833+00:00
2022-05-20T23:31:29.837864+00:00
153
false
```\nclass MKAverage {\npublic:\n #define LL long long\n struct node {\n int frq;\n LL sum;\n \n node() { frq = 0; sum = 0; }\n node(int _frq, LL _sum): frq(_frq), sum(_sum) {}\n };\n \n node Tree[100005 * 4];\n const int limit = 100000;\n\n \n node merge_segme...
1
0
[]
0
finding-mk-average
Python | Window + SortedList | O(nlogn)
python-window-sortedlist-onlogn-by-aryon-d5aa
addElement: O(log n)\ncalculateMKAverage: O(1)\n```\nfrom sortedcontainers import SortedList\nfrom collections import deque\n\nclass MKAverage:\n\n def init(
aryonbe
NORMAL
2022-05-14T07:30:32.717303+00:00
2022-05-14T07:34:40.380864+00:00
294
false
addElement: O(log n)\ncalculateMKAverage: O(1)\n```\nfrom sortedcontainers import SortedList\nfrom collections import deque\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.sl = SortedList()\n self.window = deque()\n self.sum = 0\n\n d...
1
0
['Python']
0
finding-mk-average
c++ bst + queue
c-bst-queue-by-taho2509-d6kl
The idea is to have addElement and calculateMKAverage both in O(log m), although it still is O(m) in the worst case: a left only(or right only) childs tree.\n\n
taho2509
NORMAL
2022-03-09T01:49:11.874616+00:00
2022-03-09T01:49:11.874647+00:00
289
false
The idea is to have addElement and calculateMKAverage both in O(log m), although it still is O(m) in the worst case: a left only(or right only) childs tree.\n\n```\nclass BST {\n struct node {\n int data;\n node* left;\n node* right;\n int lCount;\n int rCount;\n int lsum;\n...
1
0
['Binary Search Tree', 'Queue', 'C']
0
finding-mk-average
Python using SortedList add in O(N) calculate in O(1)
python-using-sortedlist-add-in-on-calcul-q4c2
Use 3 SortedList left, mid, right to maintain the smallest k elements, mid part and the biggest k elements seperately. \nWe can do insert, delete, search operat
utada
NORMAL
2021-09-16T17:56:21.038392+00:00
2021-09-16T17:56:21.038441+00:00
243
false
Use 3 SortedList `left`, `mid`, `right` to maintain the smallest k elements, mid part and the biggest k elements seperately. \nWe can do insert, delete, search operations in SortedList in O(logN). It\'s somehow like TreeSet/TreeMap in Java(but SortedList can store duplicate elements) and MultiSet in C++.\n```\nfrom sor...
1
0
[]
2
finding-mk-average
4 Heaps, 1 Queue / List | 50.00% speed
4-heaps-1-queue-list-5000-speed-by-odani-9itc
This solution is referenced from C++ 3 multisets by Izl124631x\nI don\'t know how to implement ordered set with duplicates in javascript, or lazy update / delet
odaniel
NORMAL
2021-07-03T02:37:11.803442+00:00
2021-07-04T04:15:52.757782+00:00
454
false
This solution is referenced from [C++ 3 multisets by Izl124631x](https://leetcode.com/problems/finding-mk-average/discuss/1152418/C%2B%2B-3-multisets)\nI don\'t know how to implement `ordered set` with `duplicates` in `javascript`, or `lazy update / delete` in `Heap`\nSo I will just implement and use `Heap` in a intuit...
1
0
['Heap (Priority Queue)', 'JavaScript']
1
finding-mk-average
[Python] Queue and Binary Search, add O(log m), average O(1)
python-queue-and-binary-search-add-olog-837av
\nimport bisect\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.list = [] ##Used as a sorted
rajat499
NORMAL
2021-05-17T20:33:04.300331+00:00
2021-05-17T20:33:04.300371+00:00
275
false
```\nimport bisect\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.list = [] ##Used as a sorted list. Elements are sorted in non decreasing order\n self.added = [] ##Used as a queue\n self.sum = None\n\t\t\n def addElement(self, num: in...
1
0
[]
2
finding-mk-average
Multiset Approach Easy to Understand
multiset-approach-easy-to-understand-by-3hm5e
\n#define ll long long int\nclass MKAverage {\n ll m,k,tot,midsum;\n queue<ll> dq;\n multiset<ll> low,mid,high;\npublic:\n MKAverage(int M, int K) {
ojha1111pk
NORMAL
2021-04-25T14:35:23.113112+00:00
2021-04-25T14:35:23.113146+00:00
607
false
```\n#define ll long long int\nclass MKAverage {\n ll m,k,tot,midsum;\n queue<ll> dq;\n multiset<ll> low,mid,high;\npublic:\n MKAverage(int M, int K) {\n low.clear(); // store k smallest numbers in latest stream of m numbers\n high.clear();// stores k largest numbers in latest stream of m numb...
1
0
['C', 'C++']
0
finding-mk-average
Help needed , last test case giving TLE
help-needed-last-test-case-giving-tle-by-mv73
\nclass MKAverage {\npublic:\n int m,k;\n int temp;\n multiset<int,greater<int>> s;\n vector<int> v;\n \n MKAverage(int m, int k) {\n t
sai_prasad_07
NORMAL
2021-04-13T16:26:03.238714+00:00
2021-04-13T16:26:03.238746+00:00
103
false
```\nclass MKAverage {\npublic:\n int m,k;\n int temp;\n multiset<int,greater<int>> s;\n vector<int> v;\n \n MKAverage(int m, int k) {\n this->m = m;\n this->k = k;\n v.clear();\n temp = 0;\n }\n \n void addElement(int num) {\n v.push_back(num);\n if(...
1
0
[]
1
finding-mk-average
[Javascript] 2 Segment Trees + Divide and Conquer
javascript-2-segment-trees-divide-and-co-q2c0
\nThe main idea is to create two seperate segment trees. One will store the count of the elements up to a specific element. So for example if the elements 1,2,
George_Chrysochoou
NORMAL
2021-04-13T13:37:25.574998+00:00
2021-04-15T05:06:42.354343+00:00
255
false
\nThe main idea is to create two seperate segment trees. One will store the count of the elements up to a specific element. So for example if the elements 1,2, 3, 3, 4, 4 are given then it ll look sth like\n\n0 1 2 3 4 \n**0 1 2 4 5** <=cumulative counts up to a specific element (think prefix sums [0,i] )\n\nThe other ...
1
0
['Tree', 'JavaScript']
0
finding-mk-average
Java clean code with MinMaxHeap implementation.
java-clean-code-with-minmaxheap-implemen-rhhx
O(logN) for addElement\nO(1) for calculateMKAverage\n\nclass MKAverage {\n int m , k;\n LinkedList<Integer> q = new LinkedList<>();\n MinMaxQ head = ne
yubad2000
NORMAL
2021-04-12T21:20:43.001467+00:00
2021-04-12T21:20:43.001510+00:00
238
false
O(logN) for addElement\nO(1) for calculateMKAverage\n```\nclass MKAverage {\n int m , k;\n LinkedList<Integer> q = new LinkedList<>();\n MinMaxQ head = new MinMaxQ();\n MinMaxQ body = new MinMaxQ();\n MinMaxQ tail = new MinMaxQ();\n \n public MKAverage(int m, int k) {\n this.m=m;\n th...
1
0
[]
0
finding-mk-average
[Java] Fenwick Tree + Binary Search, O(logN) for both addElement & calculateMKAverage
java-fenwick-tree-binary-search-ologn-fo-7qqq
\n// Time complexity : O(logN)\n// Space complexity : O(N)\n// N is the possible max value in stream, which is 100000 based on the description\n\nclass MKAverag
feng4
NORMAL
2021-04-12T18:31:14.540621+00:00
2021-04-12T18:31:14.540668+00:00
187
false
```\n// Time complexity : O(logN)\n// Space complexity : O(N)\n// N is the possible max value in stream, which is 100000 based on the description\n\nclass MKAverage {\n \n private int LIMIT = 100000;\n \n int m, k;\n long sum;\n Queue<Integer> q;\n BIT bit1, bit2;\n\n public MKAverage(int m, int...
1
0
[]
0
finding-mk-average
[Python 3] O(logm) LazySumHeap and PartitionSum
python-3-ologm-lazysumheap-and-partition-sd2a
\nclass LazySumHeap:\n \n def __init__(self, key):\n self.heap = []\n self.key = key\n self.c = Counter()\n self.sz = 0\n
serdes
NORMAL
2021-04-12T04:04:25.648120+00:00
2021-04-12T04:04:25.648163+00:00
115
false
```\nclass LazySumHeap:\n \n def __init__(self, key):\n self.heap = []\n self.key = key\n self.c = Counter()\n self.sz = 0\n self.sum = 0\n \n def __len__(self):\n return self.sz\n \n def clean(self):\n while self.heap and not self.c[self.heap[0][2]]:\n...
1
0
[]
0
finding-mk-average
Python3 SortedList
python3-sortedlist-by-gautamsw51-50ka
Create a SortedList srr and an array arr.\narr contains all elements in the order they are inserted.\nsrr contains last m elements of arr in sorted order.\nmids
gautamsw51
NORMAL
2021-04-11T17:54:48.575413+00:00
2021-04-11T17:54:48.575443+00:00
136
false
Create a SortedList srr and an array arr.\narr contains all elements in the order they are inserted.\nsrr contains last m elements of arr in sorted order.\nmidsum is the sum of elements of srr excluding 1st k and last k elements.\nWhile taking average size would be n = m - 2 * k\nwhenever a new element has to be insert...
1
0
[]
0
finding-mk-average
C++ Balance 3 Multisets/Heaps
c-balance-3-multisetsheaps-by-subhaj-m7je
Have 3 multisets: first multiset, middle multiset, last multiset.\n\nWe can limit the elements under consideration to \'m\'. Because we are always interested i
subhaj
NORMAL
2021-04-11T12:04:15.812086+00:00
2021-04-11T12:23:30.452805+00:00
183
false
Have 3 multisets: f*irst multiset, middle multiset, last multiset.*\n\nWe can limit the elements under consideration to \'m\'. Because we are always interested in last m elements from the numbers stream. \nThe first multiset will contain first K smallest elements. and the last multiset will contain last K elements. \n...
1
0
[]
0
finding-mk-average
c++ BIT | 300 ms
c-bit-300-ms-by-cxky-7pvk
I had similar idea just with @ye15. So I posted c++ version here\n\nclass MKAverage {\npublic:\n int m, k;\n deque<int> dq;\n long sum[100003], cnt[100
cxky
NORMAL
2021-04-11T11:39:35.700293+00:00
2021-04-11T11:49:27.469771+00:00
408
false
I had similar idea just with @ye15. So I posted c++ version here\n```\nclass MKAverage {\npublic:\n int m, k;\n deque<int> dq;\n long sum[100003], cnt[100003];\n MKAverage(int m, int k) {\n memset(sum, 0, sizeof(sum));\n this->m = m, this->k = k;\n }\n void update(int num, int val) {\n ...
1
0
['C', 'Binary Tree']
1
finding-mk-average
[C++] three multisets
c-three-multisets-by-sky_io-tcmv
idea\n\n store the last m elements in three sets: L, M, R, the sum of elements in M is S\n\n every time we add a new element, besides an insertion operation, we
sky_io
NORMAL
2021-04-11T06:16:05.323195+00:00
2021-04-11T06:17:08.962821+00:00
113
false
## idea\n\n* store the last m elements in three sets: `L, M, R`, the sum of elements in M is `S`\n\n* every time we add a new element, besides an insertion operation, we may also need to erase one extra element.\n\n* after the insertion and erasion, adjust L, M; adjust M, R\n\n\t(I added an `int adjust(multiset<int> &a...
1
0
[]
0
finding-mk-average
[Python3] Deque and SortedList
python3-deque-and-sortedlist-by-grokus-az1m
```\nfrom sortedcontainers import SortedList\n\n\nclass MKAverage:\n\n def init(self, m: int, k: int):\n self.m = m\n self.k = k\n self.
grokus
NORMAL
2021-04-11T04:41:12.607797+00:00
2021-04-11T04:41:12.607824+00:00
140
false
```\nfrom sortedcontainers import SortedList\n\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.m = m\n self.k = k\n self.q = deque()\n self.mid = SortedList()\n self.total = 0\n\n def addElement(self, num: int) -> None:\n self.q.append(num)\n sel...
1
0
[]
0
finding-mk-average
[C#] solution O(M^2) using List
c-solution-om2-using-list-by-micromax-pqym
C# does not have any sorted tree classes that allow duplicate elements. So fast workaround is maintain a sorted List with small optimization for large M.\n\nWe
micromax
NORMAL
2021-04-11T04:15:06.897912+00:00
2021-04-11T06:29:52.120651+00:00
124
false
C# does not have any sorted tree classes that allow duplicate elements. So fast workaround is maintain a sorted List with small optimization for large M.\n\nWe maintain sorted list of all nums in current sliding window. To maintain expiration of numbers in sliding window we can use queue.\nWhen new number arrives we do...
1
0
[]
0
finding-mk-average
debug avl tree - python3 (not a solution)
debug-avl-tree-python3-not-a-solution-by-e1fq
i did not get it during the contest using the following avl tree structure (which should be more sophisticated than a preorder traversal but i ran out of time),
mikeyliu
NORMAL
2021-04-11T04:13:51.903254+00:00
2021-04-11T04:15:02.755236+00:00
95
false
i did not get it during the contest using the following avl tree structure (which should be more sophisticated than a preorder traversal but i ran out of time), any idea on what went wrong? the avl tree is from geeks4geeks.\n```\nclass TreeNode:\n def __init__(self, val):\n self.val = val\n self.left =...
1
0
[]
1
finding-mk-average
Python SortedList and Deque
python-sortedlist-and-deque-by-vlee-786a
\nimport collections\nimport math\nimport statistics\nfrom typing import Deque\n\nimport sortedcontainers\n\n\nclass MKAverage:\n def __init__(self, m: int,
vlee
NORMAL
2021-04-11T04:03:05.435967+00:00
2021-04-11T04:03:05.435994+00:00
309
false
```\nimport collections\nimport math\nimport statistics\nfrom typing import Deque\n\nimport sortedcontainers\n\n\nclass MKAverage:\n def __init__(self, m: int, k: int):\n self.m: int = m\n self.k: int = k\n self.sl = sortedcontainers.SortedList()\n self.dq: Deque[int] = collections.deque(...
1
1
['Queue', 'Python3']
1
finding-mk-average
Java TreeMap and Queue
java-treemap-and-queue-by-disha-mdfc
IntuitionSince it needs elements to be in sorted order so that the largest and smallest k elements can be ignored, and there can also be duplicate elements, we
disha
NORMAL
2025-03-08T17:07:42.801886+00:00
2025-03-08T17:07:42.801886+00:00
9
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Since it needs elements to be in sorted order so that the largest and smallest k elements can be ignored, and there can also be duplicate elements, we will use TreeMap. And in order to remove oldest element when more than m elements are ins...
0
0
['Java']
0
finding-mk-average
Short Interview friendly solution - Accepted
short-interview-friendly-solution-accept-2mcd
Intuition Use SortedList to keep track of sorted elements in window whenever a new element is added. Calculate the average in current window and return answer.
nishantapatil3
NORMAL
2025-02-19T07:22:24.744343+00:00
2025-02-19T07:22:24.744343+00:00
20
false
# Intuition 1. Use SortedList to keep track of sorted elements in window whenever a new element is added. 2. Calculate the average in current window and return answer. # Complexity - Time complexity: O(log m) - Space complexity: O(m) # Code ```python3 [] class MKAverage: def __init__(self, m: int, k: int): ...
0
0
['Python3']
0
finding-mk-average
◈ Python ◈ Ordered Set ◈ Data Stream ◈ Design
python-ordered-set-data-stream-design-by-cfrx
Glossary: m: The number of most recent elements to consider. k: The number of smallest and largest elements to exclude. arr: The array storing all added numbers
zurcalled_suruat
NORMAL
2025-02-12T06:42:40.729458+00:00
2025-02-12T07:20:22.121590+00:00
7
false
Glossary: - $m$: The number of most recent elements to consider. - $k$: The number of smallest and largest elements to exclude. - $arr$: The array storing all added numbers. - $ss$: The sorted set storing the last $m$ elements. - $ix$: The index of the last added element in $arr$. - $sm$: The sum of the elements in the...
0
0
['Design', 'Data Stream', 'Ordered Set', 'Python3']
0
finding-mk-average
TreeMap + Queue
treemap-queue-by-up41guy-ldnt
https://carefree-ladka.github.io/js.enigma/docs/DSA/TreeMap https://carefree-ladka.github.io/js.enigma/docs/DSA/TreeSet https://carefree-ladka.github.io/js.enig
Cx1z0
NORMAL
2025-01-14T16:44:55.779807+00:00
2025-01-14T16:44:55.779807+00:00
11
false
https://carefree-ladka.github.io/js.enigma/docs/DSA/TreeMap https://carefree-ladka.github.io/js.enigma/docs/DSA/TreeSet https://carefree-ladka.github.io/js.enigma/docs/DSA/SortedList and many more ... . I built this site to revsie topics for last minute . The implementation is quite efficient ![image.png](https:/...
0
0
['Design', 'Queue', 'Data Stream', 'Ordered Set', 'JavaScript']
0
finding-mk-average
4 heaps python solution
4-heaps-python-solution-by-user0549z-nqxn
\n# Approach\nUse 4 heaps\n max heap for lowwer k elements\n max and max heaps for middle elements\n min heap for higher k elements\n\n\n# Complexity\n addEleme
user0549Z
NORMAL
2024-11-19T14:19:05.384756+00:00
2024-11-19T14:19:05.384793+00:00
24
false
\n# Approach\nUse 4 heaps\n* max heap for lowwer k elements\n* max and max heaps for middle elements\n* min heap for higher k elements\n\n\n# Complexity\n* addElement - O(log(n))\n* calculateMKAverage - O(1)\n\n- Space complexity:\n* for heaps = 4*O(m)\n* for presence checker map = 3*O(m)\n* sor stack = O(m)\n* Total -...
0
0
['Python3']
0
finding-mk-average
1825. Finding MK Average.cpp
1825-finding-mk-averagecpp-by-202021gane-mwkr
Code\n\nclass MKAverage {\npublic:\n set<pair<int, int>, greater<>>MaxHeap;\n set<pair<int, int>>MinHeap;\n set<pair<int, int>>MidHeap;\n queue<int>
202021ganesh
NORMAL
2024-10-16T10:24:03.846104+00:00
2024-10-16T10:24:03.846128+00:00
10
false
**Code**\n```\nclass MKAverage {\npublic:\n set<pair<int, int>, greater<>>MaxHeap;\n set<pair<int, int>>MinHeap;\n set<pair<int, int>>MidHeap;\n queue<int>vals;\n int M, K;\n int count;\n long long Sum;\n int midLength;\n MKAverage(int m, int k) {\n M = m;\n K = k;\n Sum ...
0
0
['C']
0
finding-mk-average
scala SortedSet
scala-sortedset-by-vititov-3h4k
scala []\nclass MKAverage(_m: Int, _k: Int) {\n import scala.util.chaining._\n import collection.mutable\n val (lo,mid,hi) = (mutable.TreeSet.empty[(Int,Int)
vititov
NORMAL
2024-10-14T00:03:38.197446+00:00
2024-10-14T00:03:38.197465+00:00
3
false
```scala []\nclass MKAverage(_m: Int, _k: Int) {\n import scala.util.chaining._\n import collection.mutable\n val (lo,mid,hi) = (mutable.TreeSet.empty[(Int,Int)],mutable.TreeSet.empty[(Int,Int)],mutable.TreeSet.empty[(Int,Int)])\n val q = mutable.Queue.empty[(Int,Int)]\n var (sumMid,i) = (0,0)\n def toLo():Unit =...
0
0
['Design', 'Queue', 'Data Stream', 'Ordered Set', 'Scala']
0