LRU(Least Recently Used) Algorithm : 가장 오래 참조되지 않은 페이지를 교체하는 기법
Queue는 중간에 있는 값을 삭제할 수 없기 때문에 LinkedList를 사용하여 구현하였다.
cache hit
cache miss
import java.util.*;
class Solution {
public int solution(int cacheSize, String[] cities) {
if(cacheSize == 0)
return cities.length * 5;
int answer = 0;
LinkedList<String> list = new LinkedList<>();
for(String city : cities){
String cityName = city.toLowerCase();
int idx = list.indexOf(cityName);
if(idx >= 0){
answer += 1;
list.remove(idx);
}
else{
answer += 5;
if(list.size() >= cacheSize){
list.remove(0);
}
}
list.offer(cityName);
}
return answer;
}
}