2018 KAKAO BLIND RECRUITMENT - 캐시 (Level 2)

 

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


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from collections import deque

def solution(cacheSize, cities):
    # 1.1 Do not use cache
    if cacheSize == 0:
        return 5*len(cities)

    # 1.2 Implement cache
    cache = deque(maxlen=cacheSize)

    # 2. Search
    elapsed_time = 0
    for city in (city.lower() for city in cities):
        if city in cache:
            elapsed_time += 1
            cache.remove(city)
        else:
            elapsed_time += 5
        cache.appendleft(city)
        # print(cache)
    return elapsed_time

Complexity

$O(|\text{cities}|\times \text{cacheSize})$