Minimum Time to Complete All Tasks - Problem

Imagine you're managing a smart computer that can run unlimited tasks simultaneously, but you want to minimize its power consumption by turning it on only when absolutely necessary.

You're given a list of tasks where each task is represented as [start_time, end_time, duration]. This means:

  • Task i must run for exactly duration[i] seconds
  • It can only run within the time window [start_time[i], end_time[i]]
  • The task doesn't need to run continuously - it can be split across multiple time segments

Goal: Find the minimum total time the computer needs to be powered on to complete all tasks.

Key insight: Tasks can overlap in time, and when they do, the computer can work on multiple tasks simultaneously without additional power cost!

Input & Output

basic_example.py โ€” Python
$ Input: tasks = [[2,3,1],[4,5,1],[1,5,2]]
โ€บ Output: 3
๐Ÿ’ก Note: Task 1 needs 1 unit in [2,3], Task 2 needs 1 unit in [4,5], Task 3 needs 2 units in [1,5]. Optimal schedule: run at times 3,4,5. Task 1 uses time 3, Task 3 uses times 4,5, Task 2 uses time 5 (overlaps with Task 3). Total: 3 time units.
overlap_example.py โ€” Python
$ Input: tasks = [[1,3,2],[2,5,3],[5,6,1]]
โ€บ Output: 4
๐Ÿ’ก Note: Task 1 needs 2 units in [1,3], Task 2 needs 3 units in [2,5], Task 3 needs 1 unit in [5,6]. Schedule: Task 1 at times 2,3; Task 2 at times 3,4,5; Task 3 at time 5. Total unique times: 2,3,4,5 = 4 units.
single_task.py โ€” Python
$ Input: tasks = [[7,10,1]]
โ€บ Output: 1
๐Ÿ’ก Note: Only one task requiring 1 unit of time in window [7,10]. Computer needs to run for exactly 1 time unit (can pick any time from 7 to 10).

Constraints

  • 1 โ‰ค tasks.length โ‰ค 2000
  • 1 โ‰ค starti โ‰ค endi โ‰ค 2000
  • 1 โ‰ค durationi โ‰ค endi - starti + 1
  • Key constraint: Each task's duration cannot exceed its time window length

Visualization

Tap to expand
Computer Task Scheduling OptimizationTime:123456Input Tasks:Task 1: [2,3] need 1Task 2: [4,5] need 1Task 3: [1,5] need 2Optimal Schedule (Greedy):1. Sort by deadline: Task 1 (end=3), Task 3 (end=5), Task 2 (end=5)2. Schedule Task 1 late in its window:T13. Schedule Task 3 late in its window:T3T34. Task 2 reuses time slot 5:T2Result: Computer runs for 3 time unitsTimes 3, 4, 5 - Tasks 2 and 3 share time slot 5
Understanding the Visualization
1
Input Tasks
Each task has a time window [start, end] and required duration
2
Sort by Deadline
Process tasks in order of earliest deadline first
3
Schedule Late
For each task, assign time slots as late as possible in its window
4
Maximize Overlap
Later tasks can reuse already-scheduled time slots
Key Takeaway
๐ŸŽฏ Key Insight: By sorting tasks by deadline and scheduling each task as late as possible in its window, we maximize opportunities for time slot sharing between overlapping tasks, minimizing total computer runtime.
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 25
42.8K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen