본문 바로가기
Engineering WIKI/Programmers

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

by wonos 2022. 5. 30.
 

코딩테스트 연습 - [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

내 풀이

def solution(cacheSize, cities):
    answer = 0
    
    cache_list = list()
    
    for city in cities:
        city = city.lower()
        # Miss
        if city not in cache_list: 
            
            if len(cache_list) < cacheSize:
                cache_list.append(city)
            elif cacheSize != 0:
                cache_list.pop(0)
                cache_list.append(city)
                
            answer += 5
        else:
            cache_list.pop(cache_list.index(city))
            cache_list.append(city)
            
            answer += 1
        
    
    return answer

다른사람 풀이

def solution(cacheSize, cities):
    import collections
    cache = collections.deque(maxlen=cacheSize)
    time = 0
    for i in cities:
        s = i.lower()
        if s in cache:
            cache.remove(s)
            cache.append(s)
            time += 1
        else:
            cache.append(s)
            time += 5
    return time