https://school.programmers.co.kr/learn/courses/30/lessons/17680
LRU 방식을 이용한 문제이다.
예전에는 평범하게 List(stack)로만 접근했다.
def solution(cacheSize, cities):
answer = 0
cache = []
if cacheSize == 0:
return 5*len(cities)
else:
for i in range(len(cities)):
if cities[i].lower() not in cache:
if len(cache) < cacheSize:
cache.append(cities[i].lower())
elif len(cache) == cacheSize:
cache.pop(0)
cache.append(cities[i].lower())
answer += 5
else:
cache.remove((cities[i].lower()))
cache.append(cities[i].lower())
answer += 1
return answer
두번째 푸는 이 문제, 다른 방식인 deque(queue)로 접근해봤다.
from collections import deque
def solution(cacheSize, cities):
answer = 0
if cacheSize == 0:
return 5 * len(cities)
else:
cache = []
cache = deque(cache)
for i in range(len(cities)):
if cities[i].lower() not in cache:
if len(cache) < cacheSize:
cache.append(cities[i].lower())
elif len(cache) == cacheSize:
cache.popleft()
cache.append(cities[i].lower())
answer += 5
else:
cache.remove(cities[i].lower())
cache.append(cities[i].lower())
answer += 1
return answer
[Programmers][Python] 프로세스 (1) | 2024.01.10 |
---|---|
[LeetCode][Python] 1235. Maximum Profit in Job Scheduling (0) | 2024.01.06 |
[LeetCode][Python]300. Longest Increasing Subsequence (1) | 2024.01.05 |
[Programmers][Python] 의상 (0) | 2024.01.04 |
[LeetCode][Python]2870. Minimum Number of Operations to Make Array Empty (0) | 2024.01.04 |
댓글 영역