- 코딩테스트 연습 > 2018 KAKAO BLIND RECRUITMENT > [1차] 캐시
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드1
from collections import deque
def solution(cacheSize, cities):
answer = 0
cache = deque()
if cacheSize == 0:
answer = 5*len(cities)
else:
for city in cities:
city = city.lower() # 대소문자 구분X
if city in cache: # cache hit
answer+=1
cache.remove(city)
cache.append(city)
else: # cache miss
answer+=5
if len(cache) >= cacheSize:
cache.popleft()
cache.append(city)
else:
cache.append(city)
return answer
코드2
def solution(cacheSize, cities):
import collections
cache = collections.deque(maxlen=cacheSize)
time = 0
for i in cities:
s = i.lower()
if s in cache:
cache.remove(s)
cache.append(s)
time += 1
else:
cache.append(s)
time += 5
return time
풀이
LRU를 직접짜는 문제
Least Recently Used 알고리즘

참고
https://rangsub.tistory.com/124
[Algorithm] LRU(Least Recently Used) 알고리즘
INTRO 페이지 교체 알고리즘에는 다양한 알고리즘이 있다. FIFO, LFU, Count-Base ... 이 중 LRU 알고리즘에 대해 소개한다. 1. LRU 알고리즘이란? ◆ 가장 오랜 시간 사용되지 않은 페이지를 교체하는 운영
rangsub.tistory.com
'Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스/python/Lv2] 뉴스 클러스터링 (0) | 2023.12.16 |
---|---|
[프로그래머스/python/Lv2] 튜플 (0) | 2023.12.16 |
[프로그래머스/python/Lv1] 비밀지도 (0) | 2023.12.14 |
[프로그래머스/python/Lv1] 숫자 문자열과 영단어 (0) | 2023.12.14 |
[BFS] Python 43162 네트워크 (0) | 2023.05.30 |