Single-Threaded CPU - Problem
Single-Threaded CPU Task Scheduler
Imagine you're managing a single-core CPU that needs to efficiently process multiple tasks. You have
The CPU follows these intelligent scheduling rules:
• Idle State: If no tasks are available, the CPU waits
• Task Selection: Among available tasks, pick the one with shortest processing time
• Tie-Breaking: If processing times are equal, choose the task with smallest original index
• Non-Preemptive: Once started, a task runs to completion
• Instant Switching: CPU can immediately start the next task
Goal: Return the order in which the CPU processes all tasks, demonstrating optimal task scheduling in action!
Imagine you're managing a single-core CPU that needs to efficiently process multiple tasks. You have
n tasks labeled from 0 to n - 1, where each task is represented by tasks[i] = [enqueueTime_i, processingTime_i].The CPU follows these intelligent scheduling rules:
• Idle State: If no tasks are available, the CPU waits
• Task Selection: Among available tasks, pick the one with shortest processing time
• Tie-Breaking: If processing times are equal, choose the task with smallest original index
• Non-Preemptive: Once started, a task runs to completion
• Instant Switching: CPU can immediately start the next task
Goal: Return the order in which the CPU processes all tasks, demonstrating optimal task scheduling in action!
Input & Output
example_1.py — Basic Task Scheduling
$
Input:
tasks = [[1,2],[2,4],[3,2],[4,1]]
›
Output:
[0,2,3,1]
💡 Note:
At t=1, only task 0 is available (process for 2 units until t=3). At t=3, tasks 1 and 2 are available; choose task 2 (shorter processing time, process until t=5). At t=5, tasks 1 and 3 are available; choose task 3 (shorter processing time, process until t=6). Finally process task 1 from t=6 to t=10.
example_2.py — Tie-Breaking by Index
$
Input:
tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
›
Output:
[4,3,2,0,1]
💡 Note:
All tasks become available at t=7. Since they all have the same enqueue time, we process them in order of processing time: task 4 (2 units), task 3 (4 units), task 2 (5 units), task 0 (10 units), task 1 (12 units).
example_3.py — CPU Idle Time
$
Input:
tasks = [[5,2],[7,2]]
›
Output:
[0,1]
💡 Note:
At t=0, no tasks are available so CPU is idle until t=5. Process task 0 from t=5 to t=7. At t=7, task 1 becomes available and is processed from t=7 to t=9.
Constraints
- 1 ≤ tasks.length ≤ 105
- 1 ≤ enqueueTimei, processingTimei ≤ 109
- All task indices are unique and range from 0 to n-1
Visualization
Tap to expand
Understanding the Visualization
1
Sort by Arrival
Arrange all tasks chronologically by their enqueue time
2
Build Available Pool
Add all tasks that have arrived by current time to the priority queue
3
Select Optimal Task
Pick task with shortest processing time (break ties by original index)
4
Execute and Advance
Process the selected task and advance CPU time by processing duration
5
Repeat Until Complete
Continue until all tasks are processed, handling CPU idle time efficiently
Key Takeaway
🎯 Key Insight: The optimal solution combines chronological task processing (sorting) with efficient priority management (min-heap) to achieve O(n log n) time complexity while handling CPU idle time intelligently.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code