Lv.3heappriority-queue
Running Median
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Numbers arrive in the order of nums. After each arrival report the median so far: with k numbers, the
-th smallest (the lower middle for even k).
Examples
| nums | result |
|---|---|
[1, 5, 2, 10, -99, 7, 5] | [1, 1, 2, 2, 2, 2, 5] |
[3, 3, 3] | [3, 3, 3] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(nums: int[]) → int[]
Constraints
-
nums.length,nums[i]
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| nums | Expected |
|---|---|
[1,5,2,10,-99,7,5] | [1,1,2,2,2,2,5] |
[3,3,3] | [3,3,3] |
Hint
Hint
Keep a max-heap of the lower half and a min-heap of the upper half, with the lower half equal in size or one larger; its top is the answer. Re-selecting the median from scratch each time is at least .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1478 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 7 | verdict only |
| performance | 60 | 4 | verdict only |