Solving Leetcode Interviews in Seconds with AI: Task Scheduler II
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2365" 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 a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task. You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed. Each day, until all tasks have been completed, you must either: Complete the next task from tasks, or Take a break. Return the minimum number of days needed to complete all tasks. Example 1: Input: tasks = [1,2,1,2,3,1], space = 3 Output: 9 Explanation: One way to complete all tasks in 9 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. Day 7: Take a break. Day 8: Complete the 4th task. Day 9: Complete the 5th task. It can be shown that the tasks cannot be completed in less than 9 days. Example 2: Input: tasks = [5,8,8,5], space = 2 Output: 6 Explanation: One way to complete all tasks in 6 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. It can be shown that the tasks cannot be completed in less than 6 days. Constraints: 1 <= tasks.length <= 105 1 <= tasks[i] <= 109 1 <= space <= tasks.length
Explanation
- Iterate through the
tasksarray, keeping track of the last completed day for each task type using a dictionary.- For each task, determine the earliest day it can be completed based on the
spaceconstraint and the last completed day of that task type. If a break is needed, increment the current day counter. - Update the last completed day for the current task type and continue until all tasks are processed.
- For each task, determine the earliest day it can be completed based on the
- Runtime Complexity: O(n), Storage Complexity: O(k) where n is the number of tasks and k is the number of distinct task types.
Code
def task_scheduler(tasks, space):
"""
Calculates the minimum number of days needed to complete all tasks with the given space constraint.
Args:
tasks: A list of positive integers representing the tasks.
space: The minimum number of days that must pass after the completion of a task before another task of the same type can be performed.
Returns:
The minimum number of days needed to complete all tasks.
"""
last_completed = {} # Dictionary to store the last completed day for each task type
current_day = 0
for task in tasks:
if task in last_completed:
last_day = last_completed[task]
available_day = last_day + space + 1
if current_day < available_day:
current_day = available_day
current_day += 1
last_completed[task] = current_day -1 # Update last completed day
return current_day