Skip to content
BytePatterns

Middle Score After Each Entry

EasyTwo Heaps & K-Way Merge#two-heaps#streaming~20m

Problem

Scores arrive one at a time. After each arrival, report the middle score of everything seen so far. When the number of scores is even, report the lower of the two middle scores. Return the list of reports, and aim for better than sorting all the scores again after every arrival.

Examples

Input:  scores = [5, 15, 1, 3]
Output: [5, 5, 5, 3]
Why:    after all four arrive the sorted scores are 1, 3, 5, 15, and the lower middle is 3
Input:  scores = [2, 2, 2]
Output: [2, 2, 2]
Input:  scores = []
Output: []
Why:    edge case, no arrivals means no reports

Hints

0 / 3

Stuck on the idea rather than the code? Two Heaps: Running Median covers it.