Advertisement

Single-Threaded CPU - LeetCode 1834 Solution

Single-Threaded CPU - Complete Solution Guide

Single-Threaded CPU is LeetCode problem 1834, 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 n ​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks , where tasks[i] = [enqueueTime i , processingTime i ] means that the i ​​​​​​th ​​​​ task will be available to process at enqueueTime i and will take processingTime i 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 an

Detailed Explanation

The problem asks us to simulate a single-threaded CPU processing a set of tasks. Each task has an `enqueueTime` (when it becomes available) and a `processingTime` (how long it takes to complete). The CPU chooses tasks based on the shortest processing time first, and if there's a tie, it chooses the task with the smaller index. We need to return the order in which the CPU processes the tasks.

Solution Approach

The solution involves sorting the tasks by their enqueue time and then using a min-heap to manage available tasks. The algorithm iterates, simulating the CPU's behavior. It adds tasks to the heap as they become available and then selects the task with the shortest processing time from the heap. The current time is advanced by the processing time of the selected task, and the process continues until all tasks have been processed.

Step-by-Step Algorithm

  1. Step 1: Create a list of tuples/arrays where each tuple contains the enqueue time, processing time, and original index of the task. This preserves the original index for the final result.
  2. Step 2: Sort the list of tasks based on their enqueue times.
  3. Step 3: Initialize an empty min-heap (priority queue) to store available tasks, sorted by processing time (and index as a tiebreaker).
  4. Step 4: Initialize an empty list to store the result (the order of processed tasks).
  5. Step 5: Initialize `current_time` to 0 and `task_idx` to 0. `current_time` represents the CPU's current time, and `task_idx` points to the next task in the sorted list of tasks.
  6. Step 6: While the result list has fewer than `n` elements (where `n` is the total number of tasks), repeat steps 7-9:
  7. Step 7: Add tasks to the min-heap if their enqueue time is less than or equal to the current time. Increment `task_idx` accordingly.
  8. Step 8: If the min-heap is not empty, extract the task with the shortest processing time. Add its index to the result list, and update the current time by adding the task's processing time.
  9. Step 9: If the min-heap is empty, but there are still tasks waiting to be enqueued, advance the current time to the enqueue time of the next task. This prevents the CPU from idling unnecessarily.
  10. Step 10: Return the result list.

Key Insights

  • Insight 1: We need to keep track of the current time and which tasks are available to be processed at that time.
  • Insight 2: A min-heap (priority queue) is ideal for selecting the task with the shortest processing time from the available tasks.
  • Insight 3: Sorting the tasks by their enqueue time allows us to efficiently determine when tasks become available.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting, Heap (Priority Queue).

Companies

Asked at: DoorDash, IBM.