본문 바로가기
PROGRAMMERS

[프로그래머스/파이썬] [1차] 캐시

by kode-daks 2025. 1. 13.

내 풀이

from collections import deque


def solution(cacheSize, cities):
    # 변수 초기화
    answer = 0
    cache = deque(maxlen=cacheSize) # deque 최대 크기 설정
    
    # 대소문자 구분 없으므로 전체 lower case 변환
    def lower(x):
        return x.lower()
    cities = list(map(lower, cities))
    
    for city in cities:
        # cache에 있을 경우
        if city in cache:
            answer += 1
            cache.remove(city)
            cache.append(city)
        
        # cache에 없을 경우
        elif city not in cache:
            answer += 5
            cache.append(city)

    return answer

 

메모

알고리즘 공부해야겠다. LRU 설명은 아래 참고함.

https://dailylifeofdeveloper.tistory.com/355