Editor

Running Top Three

#287 · Heaps · Intermediate · 65 XP · about 12 min

An arcade machine shows the three best scores so far and updates the display after every game. Write top_three(scores) that takes the scores in the order the games were played and returns a list of lists, one per game: the highest three scores seen up to and including that game, from highest to lowest.

Before the third game there are fewer than three scores, so the list shows all of them. Equal scores are separate entries: two games that both scored 50 can both appear.

Examples

top_three([40, 75, 20, 90, 75])
# returns [[40], [75, 40], [75, 40, 20], [90, 75, 40], [90, 75, 75]]

top_three([5, 5, 5, 5])
# returns [[5], [5, 5], [5, 5, 5], [5, 5, 5]]

top_three([])
# returns []

Constraints

  • 0 <= len(scores) <= 100,000
  • Scores are whole numbers between -10^9 and 10^9
  • Sorting all the scores so far after every game is too slow for 100,000 games.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.