코딩 테스트/문제 풀기

[프로그래머스] 캐시

hazel_ 2021. 3. 29. 13:02

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):
    if cacheSize==0:
        return len(cities)*5

    q=deque()
    time=0

    for city in cities:
        city=city.lower()
        if city in q:
            q.remove(city)
            q.append(city)
            time+=1
        else:
            if len(q) == cacheSize:
                q.popleft()
            q.append(city)
            time+=5

    return time