📖 문제
https://programmers.co.kr/learn/courses/30/lessons/17680
코딩테스트 연습 - [1차] 캐시
3 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"] 50 3 ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"] 21 2 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Ro
programmers.co.kr
💻 코드
from collections import deque
def solution(cacheSize, cities):
dq = deque(maxlen=cacheSize)
t = 0
for city in cities:
s = city.lower()
if s in dq:
t += 1
dq.remove(s)
else:
t += 5
dq.append(s)
return t
🙌 한마디
deque는 maxlen으로 최대 길이를 설정할 수 있다. 이것이 LRU(Least Recently Used)를 구현하기에 적절했다.
'Problem Solving > 📕Programmers' 카테고리의 다른 글
[Programmers] 튜플 (Python) (0) | 2021.10.09 |
---|---|
[Programmers] n진수 게임 (Python) (0) | 2021.10.08 |
[Programmers] 뉴스 클러스터링 (Python) (0) | 2021.10.06 |
[Programmers] 파일명 정렬 (Python) (0) | 2021.10.05 |
[Programmers] 프렌즈4블록 (Python) (0) | 2021.10.04 |