LRU Algorithm


Operating systems use paging for memory management, page replacement algorithms are needed to decide which page needs to be replaced when the new page comes in. Whenever a new page is referred to and is not present in memory, the page fault occurs and the Operating System replaces one of the existing pages with a newly needed page. A sequence of pages and memory capacity is given, your task is to find the number of page faults using the Least Recently Used cache (LRU) Algorithm.

Write a function solve that have the following parameter(s):
1.) a: integers array indicating pages
2.) b: integer indicating memory capacity

Example
Input:
a = [1, 0, 4, 3, 7, 6]
b = 3
Output:
The function will return,
7

Explanation
Memory allocated with 3 pages 1, 0, 4: page fault = 3
Page number 3 is required, replaces LRU 1: page fault = 3+1 = 4
Page number 7 is required, replaces LRU 0: page fault = 4 + 1 = 5
Page number 6 is required which replaces LRU 4: page fault = 6 + 1 = 7

Constraints
• The number of pages will be greater than or equal to 1 and less than or equal to 1000.
• Capacity of memory will be greater than or equal to 4 and less than or equal to 100.