|
| 1 | +{ |
| 2 | + "problem_name": "find_median_from_data_stream", |
| 3 | + "solution_class_name": "MedianFinder", |
| 4 | + "problem_number": "295", |
| 5 | + "problem_title": "Find Median from Data Stream", |
| 6 | + "difficulty": "Hard", |
| 7 | + "topics": "Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.\n\n- For example, for `arr = [2,3,4]`, the median is `3`.\n- For example, for `arr = [2,3]`, the median is `(2 + 3) / 2 = 2.5`.\n\nImplement the MedianFinder class:\n\n- `MedianFinder()` initializes the `MedianFinder` object.\n- `void addNum(int num)` adds the integer `num` from the data stream to the data structure.\n- `double findMedian()` returns the median of all elements so far. Answers within `10^-5` of the actual answer will be accepted.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput\n[\"MedianFinder\", \"addNum\", \"addNum\", \"findMedian\", \"addNum\", \"findMedian\"]\n[[], [1], [2], [], [3], []]\nOutput\n[null, null, null, 1.5, null, 2.0]\n```\n\n**Explanation:**\n```\nMedianFinder medianFinder = new MedianFinder();\nmedianFinder.addNum(1); // arr = [1]\nmedianFinder.addNum(2); // arr = [1, 2]\nmedianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)\nmedianFinder.addNum(3); // arr = [1, 2, 3]\nmedianFinder.findMedian(); // return 2.0\n```" |
| 13 | + } |
| 14 | + ], |
| 15 | + "readme_constraints": "- `-10^5 <= num <= 10^5`\n- There will be at least one element in the data structure before calling `findMedian`.\n- At most `5 * 10^4` calls will be made to `addNum` and `findMedian`.", |
| 16 | + "readme_additional": "**Follow up:**\n\n- If all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?\n- If `99%` of all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?", |
| 17 | + "solution_imports": "", |
| 18 | + "solution_methods": [ |
| 19 | + { "name": "__init__", "parameters": "", "return_type": "None", "dummy_return": "" }, |
| 20 | + { "name": "add_num", "parameters": "num: int", "return_type": "None", "dummy_return": "" }, |
| 21 | + { "name": "find_median", "parameters": "", "return_type": "float", "dummy_return": "0.0" } |
| 22 | + ], |
| 23 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import MedianFinder", |
| 24 | + "test_class_name": "FindMedianFromDataStream", |
| 25 | + "test_helper_methods": [], |
| 26 | + "test_methods": [ |
| 27 | + { |
| 28 | + "name": "test_median_finder", |
| 29 | + "parametrize": "operations, inputs, expected", |
| 30 | + "parametrize_typed": "operations: list[str], inputs: list[list[int]], expected: list[float | None]", |
| 31 | + "test_cases": "[([\"MedianFinder\", \"addNum\", \"addNum\", \"findMedian\", \"addNum\", \"findMedian\"], [[], [1], [2], [], [3], []], [None, None, None, 1.5, None, 2.0])]", |
| 32 | + "body": "mf: MedianFinder | None = None\nresults: list[float | None] = []\nfor i, op in enumerate(operations):\n if op == \"MedianFinder\":\n mf = MedianFinder()\n results.append(None)\n elif op == \"addNum\" and mf is not None:\n mf.add_num(inputs[i][0])\n results.append(None)\n elif op == \"findMedian\" and mf is not None:\n results.append(mf.find_median())\nassert results == expected" |
| 33 | + } |
| 34 | + ], |
| 35 | + "playground_imports": "from solution import MedianFinder", |
| 36 | + "playground_test_case": "# Example test case\noperations = ['MedianFinder', 'addNum', 'addNum', 'findMedian', 'addNum', 'findMedian']\ninputs = [[], [1], [2], [], [3], []]\nexpected = [None, None, None, 1.5, None, 2.0]", |
| 37 | + "playground_execution": "mf = None\nresults: list[float | None] = []\nfor i, op in enumerate(operations):\n if op == 'MedianFinder':\n mf = MedianFinder()\n results.append(None)\n elif op == 'addNum' and mf is not None:\n mf.add_num(inputs[i][0])\n results.append(None)\n elif op == 'findMedian' and mf is not None:\n results.append(mf.find_median())\nresults", |
| 38 | + "playground_assertion": "assert results == expected" |
| 39 | +} |
0 commit comments