Proficient Level

Implement the lruCacheGetOutputSum method that simulates an LRU cache and returns the sum of get results.

You are given a cache capacity and a list of operations. Each operation is represented as [type, key, value]. Type 1 means put the key-value pair into the cache. Type 2 means get the key; the value field is ignored.

For every get operation, add the returned value to a running sum. If a key is missing, add -1. Return the final sum of all get results.

Example 1
Input:
capacity (int) = 2
operations (int[][]) = [[1,1,1],[1,2,2],[2,1,0],[1,3,3],[2,2,0],[1,4,4],[2,1,0],[2,3,0],[2,4,0]]
size (int) = 9
Return:
(int) 6
Example 2
Input:
capacity (int) = 1
operations (int[][]) = [[1,2,1],[2,2,0],[1,3,2],[2,2,0],[2,3,0]]
size (int) = 5
Return:
(int) 2
Example 3
Input:
capacity (int) = 2
operations (int[][]) = [[2,1,0],[1,1,5],[2,1,0]]
size (int) = 3
Return:
(int) 4

An LRU cache removes the least recently used key when capacity is exceeded.

Use a hash map for fast key lookup and a linked order structure to track usage from least recent to most recent. On every get or put of an existing key, move that key to the most recently used position. When inserting a new key beyond capacity, remove the least recently used key.

The problem returns the sum of get outputs so the simulation has one deterministic integer result.

Pseudocode:

function lruCacheGetOutputSum(capacity, operations, size):
    cache = empty map
    order = empty structure from least recent to most recent
    total = 0
    for each operation [type, key, value]:
        if type == 1:
            if key exists:
                update value and move key to most recent
            else:
                if cache size == capacity:
                    remove least recently used key
                insert key and value as most recent
        else:
            if key exists:
                total += cache[key]
                move key to most recent
            else:
                total += -1
    return total
Run your code to see the result.