|
| 1 | +{ |
| 2 | + "problem_name": "implement_queue_using_stacks", |
| 3 | + "solution_class_name": "MyQueue", |
| 4 | + "problem_number": "232", |
| 5 | + "problem_title": "Implement Queue using Stacks", |
| 6 | + "difficulty": "Easy", |
| 7 | + "topics": "Stack, Design, Queue", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (`push`, `peek`, `pop`, and `empty`).\n\nImplement the `MyQueue` class:\n\n- `void push(int x)` Pushes element x to the back of the queue.\n- `int pop()` Removes the element from the front of the queue and returns it.\n- `int peek()` Returns the element at the front of the queue.\n- `boolean empty()` Returns `true` if the queue is empty, `false` otherwise.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput\n[\"MyQueue\", \"push\", \"push\", \"peek\", \"pop\", \"empty\"]\n[[], [1], [2], [], [], []]\nOutput\n[null, null, null, 1, 1, false]\n```\n**Explanation:**\n```\nMyQueue myQueue = new MyQueue();\nmyQueue.push(1); // queue is: [1]\nmyQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)\nmyQueue.peek(); // return 1\nmyQueue.pop(); // return 1, queue is [2]\nmyQueue.empty(); // return false\n```" |
| 13 | + } |
| 14 | + ], |
| 15 | + "readme_constraints": "- 1 <= x <= 9\n- At most 100 calls will be made to push, pop, peek, and empty.\n- All the calls to pop and peek are valid.", |
| 16 | + "readme_additional": "**Notes:**\n- You must use **only** standard operations of a stack, which means only `push to top`, `peek/pop from top`, `size`, and `is empty` operations are valid.\n- Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.\n\n**Follow-up:** Can you implement the queue such that each operation is amortized `O(1)` time complexity? In other words, performing `n` operations will take overall `O(n)` time even if one of those operations may take longer.", |
| 17 | + "solution_imports": "", |
| 18 | + "solution_methods": [ |
| 19 | + { "name": "__init__", "parameters": "", "return_type": "None", "dummy_return": "" }, |
| 20 | + { "name": "push", "parameters": "x: int", "return_type": "None", "dummy_return": "" }, |
| 21 | + { "name": "pop", "parameters": "", "return_type": "int", "dummy_return": "0" }, |
| 22 | + { "name": "peek", "parameters": "", "return_type": "int", "dummy_return": "0" }, |
| 23 | + { "name": "empty", "parameters": "", "return_type": "bool", "dummy_return": "True" } |
| 24 | + ], |
| 25 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import MyQueue", |
| 26 | + "test_class_name": "ImplementQueueUsingStacks", |
| 27 | + "test_helper_methods": [], |
| 28 | + "test_methods": [ |
| 29 | + { |
| 30 | + "name": "test_queue_operations", |
| 31 | + "parametrize": "operations, inputs, expected", |
| 32 | + "parametrize_typed": "operations: list[str], inputs: list[list[int]], expected: list[int | None | bool]", |
| 33 | + "test_cases": "[(['MyQueue', 'push', 'push', 'peek', 'pop', 'empty'], [[], [1], [2], [], [], []], [None, None, None, 1, 1, False]), (['MyQueue', 'empty', 'push', 'peek', 'pop', 'empty'], [[], [], [1], [], [], []], [None, True, None, 1, 1, True]), (['MyQueue', 'push', 'push', 'push', 'pop', 'pop', 'peek', 'pop', 'empty'], [[], [1], [2], [3], [], [], [], [], []], [None, None, None, None, 1, 2, 3, 3, True])]", |
| 34 | + "body": "queue = None\nresults: list[int | None | bool] = []\nfor i, op in enumerate(operations):\n if op == 'MyQueue':\n queue = MyQueue()\n results.append(None)\n elif op == 'push' and queue is not None:\n queue.push(inputs[i][0])\n results.append(None)\n elif op == 'pop' and queue is not None:\n results.append(queue.pop())\n elif op == 'peek' and queue is not None:\n results.append(queue.peek())\n elif op == 'empty' and queue is not None:\n results.append(queue.empty())\nassert results == expected" |
| 35 | + } |
| 36 | + ], |
| 37 | + "playground_imports": "from solution import MyQueue", |
| 38 | + "playground_test_case": "# Example test case\nqueue = MyQueue()\nqueue.push(1)\nqueue.push(2)", |
| 39 | + "playground_execution": "result_peek = queue.peek()\nresult_pop = queue.pop()\nresult_empty = queue.empty()\nprint(f'peek: {result_peek}, pop: {result_pop}, empty: {result_empty}')", |
| 40 | + "playground_assertion": "assert result_peek == 1\nassert result_pop == 1\nassert result_empty == False" |
| 41 | +} |
0 commit comments