FIFO
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
public class Main
{
public static int pageFaults(int[] pages, int n, int cap){
HashSet<Integer> s = new HashSet<Integer>(cap);
Queue<Integer> indexes = new LinkedList<Integer>();
int page_faults = 0;
for(int i = 0; i < n; i++){
if(s.size() < cap){
if(!s.contains(pages[i])){
s.add(pages[i]);
page_faults++;
indexes.add(pages[i]);
}
}
else{
if(!s.contains(pages[i])){
int val = indexes.peek();
indexes.poll();
s.remove(val);
s.add(pages[i]);
indexes.add(pages[i]);
page_faults++;
}
}
}
return page_faults;
}
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6, 3};
int cap = 3;
System.out.println(pageFaults(pages, pages.length, cap));
}
}
Comments
Post a Comment