Music streaming app~30 min2s · 256 MBtarget O(n) time, O(capacity) spacelru · linked-lists · hashing
A streaming app shows a "continue watching" shelf with the last few titles a profile played. Each title remembers the position where the viewer stopped, so that tapping it resumes in the right place.
The shelf holds at most capacity titles. When a new title arrives and the shelf is full, the title played longest ago leaves. Previewing a title on the shelf, a peek, shows its saved position but is not a play, so it must not change the order.
Write run_shelf(capacity, commands, args).
capacity titles, ordered from the most recently played to the least.commands[i] is "play", "peek" or "rank"."play" with args[i] = [title, position]: save the position for the title and make it the most recent. A new title on a full shelf pushes out the least recent one."peek" with args[i] = [title]: return the saved position, or -1 when the title is not on the shelf. The order does not change."rank" with args[i] = [k] and k from 1 to 5: return the title in place k, counting from the most recent. Return -1 when the shelf has fewer than k titles."peek" and "rank", in order. A "play" records nothing.Input: capacity = 3, commands = ["play", "play", "play", "peek", "play", "peek", "rank", "rank", "play", "rank", "rank", "rank"], args = [[10, 100], [20, 200], [30, 300], [10], [40, 400], [10], [1], [3], [20, 250], [1], [2], [4]]
Output: [100, -1, 40, 20, 20, 40, -1]The table shows the shelf after each command, most recent title first.
| Step | Command | Shelf after the command | Answer |
|---|---|---|---|
| 1 | play 10 at 100 | 10 | |
| 2 | play 20 at 200 | 20, 10 | |
| 3 | play 30 at 300 | 30, 20, 10 | |
| 4 | peek 10 | 30, 20, 10 | 100 |
| 5 | play 40 at 400 (title 10 leaves) | 40, 30, 20 | |
| 6 | peek 10 | 40, 30, 20 | -1 |
| 7 | rank 1 | 40, 30, 20 | 40 |
| 8 | rank 3 | 40, 30, 20 | 20 |
| 9 | play 20 at 250 | 20, 40, 30 | |
| 10 | rank 1 | 20, 40, 30 | 20 |
| 11 | rank 2 | 20, 40, 30 | 40 |
| 12 | rank 4 | 20, 40, 30 | -1 |
At step 4 the peek did not save title 10 from leaving at step 5, because a peek is not a play. The output is [100, -1, 40, 20, 20, 40, -1].
Input: capacity = 1, commands = ["play", "play", "peek", "peek"], args = [[1, 10], [2, 20], [1], [2]]
Output: [-1, 20]With capacity 1, the second play pushes out the first title.
Input: capacity = 2, commands = ["peek", "rank"], args = [[5], [1]]
Output: [-1, -1]The shelf is empty, so a peek and a rank both answer -1.
1 ≤ capacity ≤ 100,000 (titles on the shelf)1 ≤ commands.length ≤ 100,000; args has the same length"play", "peek" or "rank". A play has two numbers in args[i], a peek has one title, and a rank has one number from 1 to 5.0 ≤ title, position ≤ 1,000,000A plain list that is searched and shifted on every command costs about n × capacity, up to 10^10 steps. The shelf needs constant time for each command. A hash map finds a title's place. A doubly linked list moves a title to the front and removes the last one without shifting anything.