Advertisement

Task Scheduler - LeetCode 621 Solution

Task Scheduler - Complete Solution Guide

Task Scheduler is LeetCode problem 621, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Return the minimum number of CPU intervals required to complete all tasks. Example 1: Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 Explanation: A possible sequence is: A -> B ->

Detailed Explanation

The Task Scheduler problem requires you to determine the minimum number of CPU intervals needed to execute a given list of tasks. Each task is represented by an uppercase letter. There's a cooling period `n` between executing the same task again. You can execute tasks in any order, and the CPU can be idle during an interval if no task is available for execution. The goal is to find the shortest possible schedule that completes all tasks while respecting the cooling constraint.

Solution Approach

The solution calculates the minimum length based on the frequency of the most frequent task. First, it counts the occurrences of each task. Then, it identifies the maximum frequency and the number of tasks with that maximum frequency. The minimum length is calculated using the formula: `(max_freq - 1) * (n + 1) + num_tasks_with_max_freq`. Finally, the algorithm returns the maximum of the calculated minimum length and the total number of tasks, ensuring all tasks are scheduled.

Step-by-Step Algorithm

  1. Step 1: Count the frequency of each task using a hash map (Counter in Python, HashMap in Java/C++, manual counting in C).
  2. Step 2: Find the maximum frequency among all tasks.
  3. Step 3: Count the number of tasks that have the maximum frequency.
  4. Step 4: Calculate the minimum length of the schedule using the formula: `(max_freq - 1) * (n + 1) + num_tasks_with_max_freq`.
  5. Step 5: Return the maximum of the calculated minimum length and the total number of tasks. This ensures that if there are many different tasks with low frequencies, the schedule will be long enough to accommodate them without needing idle slots.

Key Insights

  • Insight 1: The task with the highest frequency dictates the minimum length of the schedule. We need to focus on the most frequent tasks first.
  • Insight 2: The schedule can be visualized as 'slots' where the most frequent tasks are placed, separated by the cooling interval `n`. These slots may need to be padded with idle intervals.
  • Insight 3: If the number of tasks is greater than the calculated minimum length based on the most frequent task, then the total number of tasks determines the length.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, Greedy, Sorting, Heap (Priority Queue), Counting.

Companies

Asked at: DE Shaw, DoorDash, IBM, Intuit, Remitly, Roblox, Rubrik, Salesforce, Snowflake, Zeta, zeta suite.