Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-172

Self-Organizing List Move-to-Front

Problem

Compute the total cost of move-to-front accesses on a list.

items is the initial list order and accesses is the sequence of requested items, all present in items. Each access costs the item's current 1-based position, then the item moves to the front.

Return the total access cost.

Hint

Frequently accessed items migrate to the front, making their later accesses cheaper.

Test cases

case 1
in: [[1,2,3],[1,2,3]]
expected: 6
case 2
in: [[1,2,3],[3,2,1,3]]
expected: 12
case 3
in: [[1,2],[2,2,2]]
expected: 4
case 4
in: [[5],[5]]
expected: 1
Starter codePython
def move_to_front_cost(items, accesses):
    # Your code here
    pass

4 test cases · run your code against them in the editor