Leetcode 621: Task Scheduler

grid47
grid47
Exploring patterns and algorithms
Sep 5, 2024 7 min read

A glowing task list where tasks are scheduled, with optimal scheduling steps softly illuminating.
Solution to LeetCode 621: Task Scheduler Problem

Given a list of tasks and a number n, determine the minimum number of CPU intervals required to complete all tasks, respecting the constraint that the same task must be separated by at least n intervals.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of tasks (represented as uppercase English letters) and an integer n, which denotes the cooling period between the same tasks.
Example: tasks = ["A", "A", "A", "B", "B", "B"], n = 2
Constraints:
• 1 <= tasks.length <= 10^4
• tasks[i] is an uppercase English letter.
• 0 <= n <= 100
Output: Return the minimum number of CPU intervals required to execute all tasks, including idle times where no task is executed.
Example: 8
Constraints:
• The output is a single integer representing the number of intervals.
Goal: To minimize the number of CPU intervals, the algorithm should schedule tasks in a way that maximizes task completion while respecting the cooling time n.
Steps:
• 1. Count the frequency of each task.
• 2. Use a priority queue to process tasks in the order of their frequency.
• 3. For each task, perform it and then wait for n intervals before performing it again, using idle periods where necessary.
• 4. Return the total number of intervals.
Goal: The constraints include the maximum length of the task list and the cooling period n, both of which should be respected in the solution.
Steps:
• 1 <= tasks.length <= 10^4
• tasks[i] is an uppercase English letter.
• 0 <= n <= 100
Assumptions:
• The input tasks list is valid and contains only uppercase English letters.
• n is a non-negative integer representing the cooling time between tasks of the same type.
Input: tasks = ["A", "A", "A", "B", "B", "B"], n = 2
Explanation: In this example, task A and task B need to be performed in such a way that there are at least 2 intervals between each repetition of the same task. Thus, idling is required between repetitions.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus