목록LRU (2)
N
146. LRU Cache Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-v..
LRU 형태의 캐시이므로 큐 형태로 접근을 하였다. 데이터 접근이 쉽도록 벡터를 사용하였지만, 방식은 큐와 같다. 우선 cacheSize가 0이면 캐시가 불가능하므로 데이터 수 * 5를 바로 리턴하였다. 0이 아니라면 for문을 통해 cities.size()만큼 반복한다. 대소문자 구분 없이 같은 단어이면 동일하게 처리해야하므로 transform을 사용하여 모두 소문자로 바꿔주었다. 그리고 위에서 선언했던 string형 벡터 cache를 이용한다. cache에 현재 데이터가 있는지 find() 함수를 통해 찾도록 한다. 만약 itr이 cache.end()라면 cache에는 cities[i]가 없다는 뜻이므로 cache miss가 발생한다. answer += 5를 해주기 전에 cache.size()를 검사..