LRU(Least Recently Used Alogorithm) 알고리즘
- 가장 오랜 시간동안 사용하지 않은 페이지를 교체하는 알고리즘
- 캐시 교체 정책(Cache Replacement Policy) 중 하나 - 이외에 FIFO(First In First Out), LFU(Least Frequently Used)
- Cache Hit : 참조하려고 하는 메모리가 캐시에 있는 경우
- Cache Miss : 참조하려고 하는 메모리가 캐시에 없는 경우
def solution(cacheSize, cities): # 캐시크기, 도시이름 -> 총 실행시간 출력
answer = 0
# LRU : 가장 오랜 시간동안 사용하지 않은 페이지 교체 알고리즘
# hit : 참조하려고 하는 메모리가 캐시에 있는 경우
# miss : 참조하려는 메모리가 캐시에 없는 경우
cache = []
if cacheSize == 0:
return len(cities)*5
for x in range(len(cities)):
current = cities[x].lower()
if current in cache: # 캐시 안에 있는 경우
answer += 1
cache.remove(current)
cache.append(current)
else: # 캐시 안에 없는 경우
if len(cache) == cacheSize:
cache.pop(0)
cache.append(current)
answer += 5
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[Python] 프로그래머스 Level 2 오픈 채팅방 (0) | 2022.09.11 |
---|---|
[Python] 프로그래머스 Level 2 주차 요금 계산, defaultdict (1) | 2022.09.10 |
[Python] 프로그래머스 Level 2 k진수에서 소수 개수 구하기 (0) | 2022.09.10 |
[Python] 프로그래머스 Level 2 뉴스클러스터링, Counter (0) | 2022.09.10 |
[Python] 프로그래머스 Level 2 튜플 (0) | 2022.09.10 |