Medium
-
146. LRU Cache릿코드(LEETCODE) 2020. 2. 12. 09:59
https://leetcode.com/problems/lru-cache LRU Cache Discussion 에 있는 풀이 class LRUCache { using MyList = list; using Cache = unordered_map; int maxSize; Cache cache; MyList myList; int promote(int key, int value) { // assert key exists // move to front, O(1) myList.erase(cache[key]); myList.push_front({key, value}); // replace iterator to new begin cache[key] = myList.begin(); return value; } void e..
-
1338. Reduce Array Size to The Half릿코드(LEETCODE) 2020. 2. 11. 17:31
https://leetcode.com/problems/reduce-array-size-to-the-half 불러오는 중입니다... D 풀이 map 을사용하여 같은 요소의 가중치를 준다. 이후 vector 에 가중치값을 삽입하고 오름차순 정렬을 한다. 배열의 사이즈 절반 보다 가중치의 값이 크다면 가장 적은 숫자를 제거하여서 vector 의 크기를 절반으로 줄일수 있다. class Solution { public: int minSetSize(vector& arr) { const int size = arr.size(); map m; for (int i = 0; i < size; i++) { m[arr[i]]++; } const int half = size / 2; vector vi; for (const au..