[프로그래머스] [1차] 캐시

yewon Lee·2023년 12월 6일
0


😎 코딩테스트 연습>2018 KAKAO BLIND RECRUITMENT>[1차] 캐시


📘 문제풀이

def solution(cacheSize, cities):
    answer = 0
    cache = []
    
    if cacheSize == 0:
        return len(cities) * 5
    
    for c in cities:
        c = c.lower()
        #캐시 안에 있을 때       
        if c in cache:
            i =  cache.index(c)
            p = cache.pop(i)
            cache.append(p)
            answer += 1
        #캐시 안에 없을 때
        else:
            if len(cache) < cacheSize:
                cache.append(c)
            else:
                del cache[0]
                cache.append(c)
            answer += 5
    
    return answer
LRU 알고리즘
가장 오랫동안 교체되지 않은 페이지 교체
profile
시작

0개의 댓글