A computer system maintains an LRU (Least Recently Used) cache capable of storing at most K pages.
You are given a sequence of page requests. Initially, the cache is empty.
For every requested page:
If the page is already present in the cache, it is a cache hit.
Otherwise, it is a cache miss.
After processing all requests, determine the total number of cache hits and cache misses.
K and N — the cache capacity and the number of page requests.N integers representing the page requests.Print two integers:
hits misses
1 ≤ K ≤ 10^51 ≤ N ≤ 2 × 10^51 ≤ pages[i] ≤ 10^93 8
1 2 3 1 4 5 1 2
2 6
| Request | Cache | Result |
|---|---|---|
| 1 | 1 | Miss |
| 2 | 1 2 | Miss |
| 3 | 1 2 3 | Miss |
| 1 | 2 3 1 | Hit |
| 4 | 3 1 4 | Miss |
| 5 | 1 4 5 | Miss |
| 1 | 4 5 1 | Hit |
| 2 | 5 1 2 | Miss |
Hence, there are 2 hits and 6 misses.
2 6
1 2 3 4 5 6
0 6
Every request is for a new page, so every access results in a cache miss.
Amazon • Pending