Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Task Scheduler

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "621" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 -> idle -> A -> B -> idle -> A -> B. After 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. Example 2: Input: tasks = ["A","C","A","B","D","B"], n = 1 Output: 6 Explanation: A possible sequence is: A -> B -> C -> D -> A -> B. With a cooling interval of 1, you can repeat a task after just one other task. Example 3: Input: tasks = ["A","A","A", "B","B","B"], n = 3 Output: 10 Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B. There 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. Constraints: 1 <= tasks.length <= 104 tasks[i] is an uppercase English letter. 0 <= n <= 100

Explanation

Here's a breakdown of the approach and the Python code:

  • High-Level Approach:

    • Count task frequencies: Determine how many times each task appears.
    • Prioritize most frequent tasks: The most frequent task dictates the minimum length of the schedule due to the cooling constraint.
    • Calculate idle slots: Use the frequency of the most frequent task and n to calculate the required idle slots. Adjust for other tasks that can fill those slots.
  • Complexity:

    • Runtime: O(N), where N is the number of tasks.
    • Storage: O(1), because the frequency map stores at most 26 entries (the number of uppercase English letters).

Code

    from collections import Counter

def leastInterval(tasks, n):
    """
    Calculates the minimum number of CPU intervals required to complete the tasks.

    Args:
        tasks (list[str]): A list of CPU tasks.
        n (int): The cooling interval.

    Returns:
        int: The minimum number of CPU intervals required.
    """

    task_counts = Counter(tasks)
    max_count = max(task_counts.values())
    max_count_tasks = sum(1 for count in task_counts.values() if count == max_count)

    idle_slots = (max_count - 1) * (n + 1)
    available_tasks = len(tasks) - max_count * max_count_tasks
    idle_slots = max(0, idle_slots - available_tasks)

    return len(tasks) + idle_slots

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Task Scheduler