Count cache misses
Warm-up · Write code · about 10 min
An LRU cache holds up to capacity keys and starts empty. The keys in keys are accessed in order. An access is a hit when the key is cached, and it becomes the most recent key. Otherwise it is a miss: the key is loaded, and if the cache is full, the least recently used key is evicted first.
Return the number of misses.
Example 1
Input:
[2,[1,2,1,3,2]]
Output:
4
1 and 2 miss, 1 hits, 3 evicts 2, then 2 misses again.
Example 2
Input:
[3,[1,2,3,1,2,3]]
Output:
3
Only the first access of each key misses.
Constraints
- 1 ≤ capacity ≤ 1,000
- 0 ≤ keys.length ≤ 10,000
- 0 ≤ keys[i] ≤ 1,000,000
Learn the idea first: LRU and LFU caches, a visual lesson.
More LRU and LFU caches problems
- Choose the structures Warm-up
- Predict evictions Standard
- LRU cache Standard
- Spot the bug: get forgets the order Standard
- LFU cache Challenge