캐시 - python(programmers)

참치돌고래·2021년 9월 7일
0

알고리즘

목록 보기
27/36
post-custom-banner

https://programmers.co.kr/learn/courses/30/lessons/17680#



from collections import deque
def solution(cacheSize, cities):
    answer = 0
    
    q = []
    cache = deque(q)
    
    if cacheSize == 0:
        return len(cities) * 5
    
    for city in cities:
        city = city.lower()
        
        if city not in cache:
            if len(cache) == cacheSize:
                cache.popleft()
                cache.append(city)
            else:
                cache.append(city)
            answer +=5
        elif city in cache:
            idx=list(cache).index(city)
            del cache[idx]
            cache.append(city)
            answer +=1
                    
    
            
    
                
        
    return answer
profile
안녕하세요
post-custom-banner

0개의 댓글