Skip to content

Commit 3d1ef30

Browse files
committed
+ problem 1825
1 parent eee905d commit 3d1ef30

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 1825. Finding MK Average
2+
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.
3+
4+
The **MKAverage** can be calculated using these steps:
5+
6+
1. 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.
7+
2. Remove the smallest `k` elements and the largest `k` elements from the container.
8+
3. Calculate the average value for the rest of the elements **rounded down to the nearest integer**.
9+
10+
Implement the `MKAverage` class:
11+
12+
* `MKAverage(int m, int k)` Initializes the **MKAverage** object with an empty stream and the two integers `m` and `k`.
13+
* `void addElement(int num)` Inserts a new element `num` into the stream.
14+
* `int calculateMKAverage()` Calculates and returns the **MKAverage** for the current stream **rounded down to the nearest integer**.
15+
16+
#### Example 1:
17+
<pre>
18+
<strong>Input:</strong>
19+
["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]
20+
[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]
21+
<strong>Output:</strong>
22+
[null, null, null, -1, null, 3, null, null, null, 5]
23+
<strong>Explanation:</strong>
24+
MKAverage obj = new MKAverage(3, 1);
25+
obj.addElement(3); // current elements are [3]
26+
obj.addElement(1); // current elements are [3,1]
27+
obj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.
28+
obj.addElement(10); // current elements are [3,1,10]
29+
obj.calculateMKAverage(); // The last 3 elements are [3,1,10].
30+
// After removing smallest and largest 1 element the container will be [3].
31+
// The average of [3] equals 3/1 = 3, return 3
32+
obj.addElement(5); // current elements are [3,1,10,5]
33+
obj.addElement(5); // current elements are [3,1,10,5,5]
34+
obj.addElement(5); // current elements are [3,1,10,5,5,5]
35+
obj.calculateMKAverage(); // The last 3 elements are [5,5,5].
36+
// After removing smallest and largest 1 element the container will be [5].
37+
// The average of [5] equals 5/1 = 5, return 5
38+
</pre>
39+
40+
#### Constraints:
41+
* <code>3 <= m <= 10<sup>5</sup></code>
42+
* `1 <= k*2 < m`
43+
* <code>1 <= num <= 10<sup>5</sup></code>
44+
* At most <code>10<sup>5</sup></code> calls will be made to `addElement` and `calculateMKAverage`.
45+
46+
## Solutions (Python)
47+
48+
### 1. Solution
49+
```Python
50+
from sortedcontainers import SortedList
51+
52+
53+
class MKAverage:
54+
55+
def __init__(self, m: int, k: int):
56+
self.m = m
57+
self.k = k
58+
self.sum = 0
59+
self.queue = collections.deque()
60+
self.container = SortedList()
61+
62+
def addElement(self, num: int) -> None:
63+
if len(self.queue) < self.m:
64+
self.queue.append(num)
65+
self.container.add(num)
66+
if len(self.queue) == self.m:
67+
self.sum = sum(self.container[self.k:-self.k])
68+
return
69+
70+
if num < self.container[self.k]:
71+
self.sum -= self.container[-self.k - 1]
72+
self.container.add(num)
73+
self.sum += self.container[self.k]
74+
elif num < self.container[-self.k]:
75+
self.sum += num
76+
self.container.add(num)
77+
self.sum -= self.container[-self.k - 1]
78+
else:
79+
self.container.add(num)
80+
self.queue.append(num)
81+
82+
if self.queue[0] < self.container[self.k]:
83+
self.sum += self.container[-self.k - 1]
84+
self.sum -= self.container[self.k]
85+
elif self.queue[0] < self.container[-self.k - 1]:
86+
self.sum += self.container[-self.k - 1]
87+
self.sum -= self.queue[0]
88+
self.container.discard(self.queue.popleft())
89+
90+
def calculateMKAverage(self) -> int:
91+
if len(self.queue) < self.m:
92+
return -1
93+
94+
return self.sum // (self.m - self.k * 2)
95+
96+
97+
# Your MKAverage object will be instantiated and called as such:
98+
# obj = MKAverage(m, k)
99+
# obj.addElement(num)
100+
# param_2 = obj.calculateMKAverage()
101+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 1825. 求出 MK 平均值
2+
给你两个整数 `m``k` ,以及数据流形式的若干整数。你需要实现一个数据结构,计算这个数据流的 **MK 平均值**
3+
4+
**MK 平均值** 按照如下步骤计算:
5+
6+
1. 如果数据流中的整数少于 `m` 个,**MK 平均值**`-1` ,否则将数据流中最后 `m` 个元素拷贝到一个独立的容器中。
7+
2. 从这个容器中删除最小的 `k` 个数和最大的 `k` 个数。
8+
3. 计算剩余元素的平均值,并 **向下取整到最近的整数**
9+
10+
请你实现 `MKAverage` 类:
11+
12+
* `MKAverage(int m, int k)` 用一个空的数据流和两个整数 `m``k` 初始化 **MKAverage** 对象。
13+
* `void addElement(int num)` 往数据流中插入一个新的元素 `num`
14+
* `int calculateMKAverage()` 对当前的数据流计算并返回 **MK 平均数** ,结果需 **向下取整到最近的整数**
15+
16+
#### 示例 1:
17+
<pre>
18+
<strong>输入:</strong>
19+
["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]
20+
[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]
21+
<strong>输出:</strong>
22+
[null, null, null, -1, null, 3, null, null, null, 5]
23+
<strong>解释:</strong>
24+
MKAverage obj = new MKAverage(3, 1);
25+
obj.addElement(3); // 当前元素为 [3]
26+
obj.addElement(1); // 当前元素为 [3,1]
27+
obj.calculateMKAverage(); // 返回 -1 ,因为 m = 3 ,但数据流中只有 2 个元素
28+
obj.addElement(10); // 当前元素为 [3,1,10]
29+
obj.calculateMKAverage(); // 最后 3 个元素为 [3,1,10]
30+
// 删除最小以及最大的 1 个元素后,容器为 [3]
31+
// [3] 的平均值等于 3/1 = 3 ,故返回 3
32+
obj.addElement(5); // 当前元素为 [3,1,10,5]
33+
obj.addElement(5); // 当前元素为 [3,1,10,5,5]
34+
obj.addElement(5); // 当前元素为 [3,1,10,5,5,5]
35+
obj.calculateMKAverage(); // 最后 3 个元素为 [5,5,5]
36+
// 删除最小以及最大的 1 个元素后,容器为 [5]
37+
// [5] 的平均值等于 5/1 = 5 ,故返回 5
38+
</pre>
39+
40+
#### 提示:
41+
* <code>3 <= m <= 10<sup>5</sup></code>
42+
* `1 <= k*2 < m`
43+
* <code>1 <= num <= 10<sup>5</sup></code>
44+
* `addElement``calculateMKAverage` 总操作次数不超过 <code>10<sup>5</sup></code> 次。
45+
46+
## 题解 (Python)
47+
48+
### 1. 题解
49+
```Python
50+
from sortedcontainers import SortedList
51+
52+
53+
class MKAverage:
54+
55+
def __init__(self, m: int, k: int):
56+
self.m = m
57+
self.k = k
58+
self.sum = 0
59+
self.queue = collections.deque()
60+
self.container = SortedList()
61+
62+
def addElement(self, num: int) -> None:
63+
if len(self.queue) < self.m:
64+
self.queue.append(num)
65+
self.container.add(num)
66+
if len(self.queue) == self.m:
67+
self.sum = sum(self.container[self.k:-self.k])
68+
return
69+
70+
if num < self.container[self.k]:
71+
self.sum -= self.container[-self.k - 1]
72+
self.container.add(num)
73+
self.sum += self.container[self.k]
74+
elif num < self.container[-self.k]:
75+
self.sum += num
76+
self.container.add(num)
77+
self.sum -= self.container[-self.k - 1]
78+
else:
79+
self.container.add(num)
80+
self.queue.append(num)
81+
82+
if self.queue[0] < self.container[self.k]:
83+
self.sum += self.container[-self.k - 1]
84+
self.sum -= self.container[self.k]
85+
elif self.queue[0] < self.container[-self.k - 1]:
86+
self.sum += self.container[-self.k - 1]
87+
self.sum -= self.queue[0]
88+
self.container.discard(self.queue.popleft())
89+
90+
def calculateMKAverage(self) -> int:
91+
if len(self.queue) < self.m:
92+
return -1
93+
94+
return self.sum // (self.m - self.k * 2)
95+
96+
97+
# Your MKAverage object will be instantiated and called as such:
98+
# obj = MKAverage(m, k)
99+
# obj.addElement(num)
100+
# param_2 = obj.calculateMKAverage()
101+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class MKAverage:
5+
6+
def __init__(self, m: int, k: int):
7+
self.m = m
8+
self.k = k
9+
self.sum = 0
10+
self.queue = collections.deque()
11+
self.container = SortedList()
12+
13+
def addElement(self, num: int) -> None:
14+
if len(self.queue) < self.m:
15+
self.queue.append(num)
16+
self.container.add(num)
17+
if len(self.queue) == self.m:
18+
self.sum = sum(self.container[self.k:-self.k])
19+
return
20+
21+
if num < self.container[self.k]:
22+
self.sum -= self.container[-self.k - 1]
23+
self.container.add(num)
24+
self.sum += self.container[self.k]
25+
elif num < self.container[-self.k]:
26+
self.sum += num
27+
self.container.add(num)
28+
self.sum -= self.container[-self.k - 1]
29+
else:
30+
self.container.add(num)
31+
self.queue.append(num)
32+
33+
if self.queue[0] < self.container[self.k]:
34+
self.sum += self.container[-self.k - 1]
35+
self.sum -= self.container[self.k]
36+
elif self.queue[0] < self.container[-self.k - 1]:
37+
self.sum += self.container[-self.k - 1]
38+
self.sum -= self.queue[0]
39+
self.container.discard(self.queue.popleft())
40+
41+
def calculateMKAverage(self) -> int:
42+
if len(self.queue) < self.m:
43+
return -1
44+
45+
return self.sum // (self.m - self.k * 2)
46+
47+
48+
# Your MKAverage object will be instantiated and called as such:
49+
# obj = MKAverage(m, k)
50+
# obj.addElement(num)
51+
# param_2 = obj.calculateMKAverage()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@
11041104
[1822][1822l]|[Sign of the Product of an Array][1822] |![rb]&nbsp;&nbsp;![rs]
11051105
[1823][1823l]|[Find the Winner of the Circular Game][1823] |![rs]
11061106
[1824][1824l]|[Minimum Sideway Jumps][1824] |![rs]
1107+
[1825][1825l]|[Finding MK Average][1825] |![py]
11071108
[1827][1827l]|[Minimum Operations to Make the Array Increasing][1827] |![rb]&nbsp;&nbsp;![rs]
11081109
[1828][1828l]|[Queries on Number of Points Inside a Circle][1828] |![py]
11091110
[1829][1829l]|[Maximum XOR for Each Query][1829] |![rs]
@@ -2616,6 +2617,7 @@
26162617
[1822]:Problemset/1822-Sign%20of%20the%20Product%20of%20an%20Array/README.md#1822-sign-of-the-product-of-an-array
26172618
[1823]:Problemset/1823-Find%20the%20Winner%20of%20the%20Circular%20Game/README.md#1823-find-the-winner-of-the-circular-game
26182619
[1824]:Problemset/1824-Minimum%20Sideway%20Jumps/README.md#1824-minimum-sideway-jumps
2620+
[1825]:Problemset/1825-Finding%20MK%20Average/README.md#1825-finding-mk-average
26192621
[1827]:Problemset/1827-Minimum%20Operations%20to%20Make%20the%20Array%20Increasing/README.md#1827-minimum-operations-to-make-the-array-increasing
26202622
[1828]:Problemset/1828-Queries%20on%20Number%20of%20Points%20Inside%20a%20Circle/README.md#1828-queries-on-number-of-points-inside-a-circle
26212623
[1829]:Problemset/1829-Maximum%20XOR%20for%20Each%20Query/README.md#1829-maximum-xor-for-each-query
@@ -4127,6 +4129,7 @@
41274129
[1822l]:https://leetcode.com/problems/sign-of-the-product-of-an-array/
41284130
[1823l]:https://leetcode.com/problems/find-the-winner-of-the-circular-game/
41294131
[1824l]:https://leetcode.com/problems/minimum-sideway-jumps/
4132+
[1825l]:https://leetcode.com/problems/finding-mk-average/
41304133
[1827l]:https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/
41314134
[1828l]:https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/
41324135
[1829l]:https://leetcode.com/problems/maximum-xor-for-each-query/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@
11041104
[1822][1822l]|[数组元素积的符号][1822] |![rb]&nbsp;&nbsp;![rs]
11051105
[1823][1823l]|[找出游戏的获胜者][1823] |![rs]
11061106
[1824][1824l]|[最少侧跳次数][1824] |![rs]
1107+
[1825][1825l]|[求出 MK 平均值][1825] |![py]
11071108
[1827][1827l]|[最少操作使数组递增][1827] |![rb]&nbsp;&nbsp;![rs]
11081109
[1828][1828l]|[统计一个圆中点的数目][1828] |![py]
11091110
[1829][1829l]|[每个查询的最大异或值][1829] |![rs]
@@ -2616,6 +2617,7 @@
26162617
[1822]:Problemset/1822-Sign%20of%20the%20Product%20of%20an%20Array/README_CN.md#1822-数组元素积的符号
26172618
[1823]:Problemset/1823-Find%20the%20Winner%20of%20the%20Circular%20Game/README_CN.md#1823-找出游戏的获胜者
26182619
[1824]:Problemset/1824-Minimum%20Sideway%20Jumps/README_CN.md#1824-最少侧跳次数
2620+
[1825]:Problemset/1825-Finding%20MK%20Average/README_CN.md#1825-求出-mk-平均值
26192621
[1827]:Problemset/1827-Minimum%20Operations%20to%20Make%20the%20Array%20Increasing/README_CN.md#1827-最少操作使数组递增
26202622
[1828]:Problemset/1828-Queries%20on%20Number%20of%20Points%20Inside%20a%20Circle/README_CN.md#1828-统计一个圆中点的数目
26212623
[1829]:Problemset/1829-Maximum%20XOR%20for%20Each%20Query/README_CN.md#1829-每个查询的最大异或值
@@ -4127,6 +4129,7 @@
41274129
[1822l]:https://leetcode.cn/problems/sign-of-the-product-of-an-array/
41284130
[1823l]:https://leetcode.cn/problems/find-the-winner-of-the-circular-game/
41294131
[1824l]:https://leetcode.cn/problems/minimum-sideway-jumps/
4132+
[1825l]:https://leetcode.cn/problems/finding-mk-average/
41304133
[1827l]:https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/
41314134
[1828l]:https://leetcode.cn/problems/queries-on-number-of-points-inside-a-circle/
41324135
[1829l]:https://leetcode.cn/problems/maximum-xor-for-each-query/

0 commit comments

Comments
 (0)