S E P H ' S

[Python] 캐시 본문

Algorithm/Programmers

[Python] 캐시

yoseph0310 2021. 8. 31. 22:15
 

코딩테스트 연습 - [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):
    cache = []
    answer = 0
    for city in cities:
        city = city.lower()
        
        if cacheSize:
            # cache에 없을 때 
            if not city in cache:
                if len(cache) == cacheSize:
                    cache.pop(0)
                cache.append(city)
                answer += 5
            # cache 에 존재
            else:
                cache.pop(cache.index(city))
                cache.append(city)
                answer += 1
        else:
            answer += 5   
    return answer

'Algorithm > Programmers' 카테고리의 다른 글

[Python] 주식가격  (0) 2021.09.04
[Python] 구명보트  (0) 2021.09.01
[Python] 이진 변환 반복하기  (0) 2021.08.30
[Python] 점프와 순간이동  (0) 2021.08.28
[Python] 쿼드압축 후 개수 세기  (0) 2021.08.27