Solving Leetcode Interviews in Seconds with AI: Single-Threaded CPU
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1834" 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 n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the ith task will be available to process at enqueueTimei and will take processingTimei to finish processing. You have a single-threaded CPU that can process at most one task at a time and will act in the following way: If the CPU is idle and there are no available tasks to process, the CPU remains idle. If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index. Once a task is started, the CPU will process the entire task without stopping. The CPU can finish a task then start a new one instantly. Return the order in which the CPU will process the tasks. Example 1: Input: tasks = [[1,2],[2,4],[3,2],[4,1]] Output: [0,2,3,1] Explanation: The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. Example 2: Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] Output: [4,3,2,0,1] Explanation: The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. Constraints: tasks.length == n 1 <= n <= 105 1 <= enqueueTimei, processingTimei <= 109
Explanation
Here's the solution to the task scheduling problem:
- Sort by Enqueue Time: Initially, sort the tasks based on their enqueue times. This allows us to efficiently determine which tasks are available at any given time.
- Priority Queue (Heap): Use a priority queue (min-heap) to store the available tasks. The priority is determined first by processing time (shorter processing time gets higher priority) and then by index (smaller index gets higher priority in case of ties).
Simulation: Simulate the CPU's behavior step by step. Maintain a current time. Add tasks to the priority queue as they become available. When the CPU is idle, pick the highest-priority task from the queue, process it, and update the current time.
Runtime Complexity: O(N log N), where N is the number of tasks (due to sorting and heap operations).
- Storage Complexity: O(N), where N is the number of tasks (for the sorted tasks and the priority queue).
Code
import heapq
def get_order(tasks):
"""
Processes tasks according to their enqueue and processing times using a single-threaded CPU.
Args:
tasks: A list of lists, where tasks[i] = [enqueueTime_i, processingTime_i].
Returns:
A list of integers representing the order in which the CPU processes the tasks.
"""
n = len(tasks)
indexed_tasks = []
for i in range(n):
indexed_tasks.append((tasks[i][0], tasks[i][1], i)) # (enqueueTime, processingTime, index)
indexed_tasks.sort() # Sort by enqueue time
result = []
available_tasks = [] # Priority queue (min-heap)
current_time = 0
task_index = 0
while len(result) < n:
# Add available tasks to the priority queue
while task_index < n and indexed_tasks[task_index][0] <= current_time:
enqueue_time, processing_time, index = indexed_tasks[task_index]
heapq.heappush(available_tasks, (processing_time, index))
task_index += 1
# If the CPU is idle and no tasks are available, advance the current time
if not available_tasks:
if task_index < n:
current_time = indexed_tasks[task_index][0]
else:
break
continue
# Process the task with the shortest processing time (or smallest index in case of ties)
processing_time, index = heapq.heappop(available_tasks)
result.append(index)
current_time += processing_time
return result