|
| 1 | +{ |
| 2 | + "problem_name": "lru_cache", |
| 3 | + "solution_class_name": "LRUCache", |
| 4 | + "problem_number": "146", |
| 5 | + "problem_title": "LRU Cache", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Hash Table, Linked List, Design, Doubly-Linked List", |
| 8 | + "_tags": { "list": ["grind-75"] }, |
| 9 | + "readme_description": "Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.\n\nImplement the `LRUCache` class:\n\n- `LRUCache(int capacity)` Initialize the LRU cache with positive size capacity\n- `int get(int key)` Return the value of the key if the key exists, otherwise return -1\n- `void put(int key, int value)` Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key\n\nThe functions `get` and `put` must each run in `O(1)` average time complexity.", |
| 10 | + "_readme_examples": { |
| 11 | + "list": [ |
| 12 | + { |
| 13 | + "content": "```\nInput\n[\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"]\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\nOutput\n[null, null, null, 1, null, -1, null, -1, 3, 4]\n\nExplanation\nLRUCache lRUCache = new LRUCache(2);\nlRUCache.put(1, 1); // cache is {1=1}\nlRUCache.put(2, 2); // cache is {1=1, 2=2}\nlRUCache.get(1); // return 1\nlRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}\nlRUCache.get(2); // returns -1 (not found)\nlRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}\nlRUCache.get(1); // return -1 (not found)\nlRUCache.get(3); // return 3\nlRUCache.get(4); // return 4\n```" |
| 14 | + } |
| 15 | + ] |
| 16 | + }, |
| 17 | + "readme_constraints": "- 1 <= capacity <= 3000\n- 0 <= key <= 10^4\n- 0 <= value <= 10^5\n- At most 2 * 10^5 calls will be made to get and put", |
| 18 | + "readme_additional": "", |
| 19 | + "helpers_imports": "", |
| 20 | + "helpers_content": "", |
| 21 | + "helpers_run_name": "lru_cache", |
| 22 | + "helpers_run_signature": "(solution_class: type, operations: list[str], inputs: list[list[int]])", |
| 23 | + "helpers_run_body": " cache = None\n results: list[int | None] = []\n for i, op in enumerate(operations):\n if op == 'LRUCache':\n cache = solution_class(inputs[i][0])\n results.append(None)\n elif op == 'get' and cache is not None:\n results.append(cache.get(inputs[i][0]))\n elif op == 'put' and cache is not None:\n cache.put(inputs[i][0], inputs[i][1])\n results.append(None)\n return results, cache", |
| 24 | + "helpers_assert_name": "lru_cache", |
| 25 | + "helpers_assert_signature": "(result: list[int | None], expected: list[int | None]) -> bool", |
| 26 | + "helpers_assert_body": " assert result == expected\n return True", |
| 27 | + "solution_imports": "", |
| 28 | + "solution_contents": "", |
| 29 | + "solution_class_content": "", |
| 30 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .helpers import assert_lru_cache, run_lru_cache\nfrom .solution import LRUCache", |
| 31 | + "test_content": "", |
| 32 | + "test_class_name": "LRUCache", |
| 33 | + "test_class_content": "", |
| 34 | + "_solution_methods": { |
| 35 | + "list": [ |
| 36 | + { |
| 37 | + "name": "__init__", |
| 38 | + "signature": "(self, capacity: int) -> None", |
| 39 | + "body": " # TODO: Initialize LRU cache\n pass" |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "get", |
| 43 | + "signature": "(self, key: int) -> int", |
| 44 | + "body": " # TODO: Implement get\n return -1" |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "put", |
| 48 | + "signature": "(self, key: int, value: int) -> None", |
| 49 | + "body": " # TODO: Implement put\n pass" |
| 50 | + } |
| 51 | + ] |
| 52 | + }, |
| 53 | + "_test_helper_methods": { "list": [] }, |
| 54 | + "_test_methods": { |
| 55 | + "list": [ |
| 56 | + { |
| 57 | + "name": "test_lru_cache", |
| 58 | + "signature": "(self, operations: list[str], inputs: list[list[int]], expected: list[int | None])", |
| 59 | + "parametrize": "operations, inputs, expected", |
| 60 | + "test_cases": "[(['LRUCache', 'put', 'put', 'get', 'put', 'get', 'put', 'get', 'get', 'get'], [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]], [None, None, None, 1, None, -1, None, -1, 3, 4]), (['LRUCache', 'get', 'put', 'get', 'put', 'put', 'get', 'get'], [[2], [2], [2, 6], [1], [1, 5], [1, 2], [1], [2]], [None, -1, None, -1, None, None, 2, 6]), (['LRUCache', 'put', 'get', 'put', 'get', 'get'], [[1], [2, 1], [2], [3, 2], [2], [3]], [None, None, 1, None, -1, 2])]", |
| 61 | + "body": " result, _ = run_lru_cache(LRUCache, operations, inputs)\n assert_lru_cache(result, expected)" |
| 62 | + } |
| 63 | + ] |
| 64 | + }, |
| 65 | + "playground_imports": "from helpers import run_lru_cache, assert_lru_cache\nfrom solution import LRUCache", |
| 66 | + "playground_setup": "# Example test case\noperations = ['LRUCache', 'put', 'put', 'get', 'put', 'get', 'put', 'get', 'get', 'get']\ninputs = [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\nexpected = [None, None, None, 1, None, -1, None, -1, 3, 4]", |
| 67 | + "playground_run": "result, cache = run_lru_cache(LRUCache, operations, inputs)\nprint(result)\ncache", |
| 68 | + "playground_assert": "assert_lru_cache(result, expected)" |
| 69 | +} |
0 commit comments