|
| 1 | +{ |
| 2 | + "problem_name": "task_scheduler", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "621", |
| 5 | + "problem_title": "Task Scheduler", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Array, Hash Table, Greedy, Sorting, Heap (Priority Queue), Counting", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "You are given an array of CPU `tasks`, each labeled with a letter from A to Z, and a number `n`. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of **at least** `n` intervals between two tasks with the same label.\n\nReturn the **minimum** number of CPU intervals required to complete all tasks.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput: tasks = [\"A\",\"A\",\"A\",\"B\",\"B\",\"B\"], n = 2\nOutput: 8\n```\n**Explanation:** A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.\n\nAfter completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th interval, you can do A again as 2 intervals have passed." |
| 13 | + }, |
| 14 | + { |
| 15 | + "content": "```\nInput: tasks = [\"A\",\"C\",\"A\",\"B\",\"D\",\"B\"], n = 1\nOutput: 6\n```\n**Explanation:** A possible sequence is: A -> B -> C -> D -> A -> B.\n\nWith a cooling interval of 1, you can repeat a task after just one other task." |
| 16 | + }, |
| 17 | + { |
| 18 | + "content": "```\nInput: tasks = [\"A\",\"A\",\"A\", \"B\",\"B\",\"B\"], n = 3\nOutput: 10\n```\n**Explanation:** A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.\n\nThere are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks." |
| 19 | + } |
| 20 | + ], |
| 21 | + "readme_constraints": "- `1 <= tasks.length <= 10^4`\n- `tasks[i]` is an uppercase English letter.\n- `0 <= n <= 100`", |
| 22 | + "readme_additional": "", |
| 23 | + "solution_imports": "", |
| 24 | + "solution_methods": [ |
| 25 | + { |
| 26 | + "name": "least_interval", |
| 27 | + "parameters": "tasks: list[str], n: int", |
| 28 | + "return_type": "int", |
| 29 | + "dummy_return": "0" |
| 30 | + } |
| 31 | + ], |
| 32 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution", |
| 33 | + "test_class_name": "TaskScheduler", |
| 34 | + "test_helper_methods": [ |
| 35 | + { "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" } |
| 36 | + ], |
| 37 | + "test_methods": [ |
| 38 | + { |
| 39 | + "name": "test_least_interval", |
| 40 | + "parametrize": "tasks, n, expected", |
| 41 | + "parametrize_typed": "tasks: list[str], n: int, expected: int", |
| 42 | + "test_cases": "[([\"A\", \"A\", \"A\", \"B\", \"B\", \"B\"], 2, 8), ([\"A\", \"C\", \"A\", \"B\", \"D\", \"B\"], 1, 6), ([\"A\", \"A\", \"A\", \"B\", \"B\", \"B\"], 3, 10)]", |
| 43 | + "body": "result = self.solution.least_interval(tasks, n)\nassert result == expected" |
| 44 | + } |
| 45 | + ], |
| 46 | + "playground_imports": "from solution import Solution", |
| 47 | + "playground_test_case": "# Example test case\ntasks = [\\\"A\\\", \\\"A\\\", \\\"A\\\", \\\"B\\\", \\\"B\\\", \\\"B\\\"]\nn = 2\nexpected = 8", |
| 48 | + "playground_execution": "result = Solution().least_interval(tasks, n)\nresult", |
| 49 | + "playground_assertion": "assert result == expected" |
| 50 | +} |
0 commit comments