나의 코드
def solution(cacheSize, cities):
answer = 0
data = []
if cacheSize == 0:
return len(cities) * 5
for i in cities:
i = i.lower()
if i not in data:
if len(data) == cacheSize:
data.pop(0)
data.append(i)
answer += 5
else:
data.pop(data.index(i))
data.append(i)
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
deque의 maxlen 인자를 이용
'코딩 테스트 (Coding Test)' 카테고리의 다른 글
[프로그래머스] 위장 - 파이썬 풀이 (0) | 2023.01.18 |
---|---|
[프로그래머스] 괄호 회전하기 - 파이썬 풀이 (0) | 2023.01.17 |
[프로그래머스] 멀리 뛰기 - 파이썬 풀이 (0) | 2023.01.17 |
[프로그래머스] 점프와 순간 이동 (0) | 2023.01.17 |
[프로그래머스] 예상 대진표 - 파이썬 풀이 (0) | 2023.01.17 |