11/30
Part 3 of 7 · Two sorted listsLearn and do

An update moves a key inside the list that holds it

Now level 2. update(title, score) has three cases:

The title isWhat happens to its key
waitingmoves inside waiting, so it shows in this loop by its new score
shownmoves inside shown, so it waits for the next loop
newgoes into waiting, so it shows in this loop

The steps for a known title, in this order:

  1. Build the old key from the score you have stored: (-scores[title], title).
  2. Look for it in waiting with bisect_left. If it isn't there, it's in shown.
  3. Delete it from that list.
  4. Store the new score, and insort the new key into the same list.

You need the dict of scores for step 1. A key is found by its exact value, and the list doesn't know a title's score unless you kept it somewhere. In the class, that dict is self.scores, and __init__ should store a copy, self.scores = dict(scores). The caller's dict belongs to the caller: the practice problem's tests check that an update leaves it alone. Each update is two binary searches, one delete and one insert: O(log n) to find, O(n) to shift.

Why does a new title wait in the current loop? Because it hasn't been shown in it. That holds even when every other title has already shown. The new title goes into waiting, so the next get shows it before the loop turns over.

The editor holds update over the dict and the two lists. Write update(scores, waiting, shown, title, score) to cover all three cases.