#3683
Earliest Time to Finish One Task
EasyArrayArrayMin/Max Search
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(n)Space O(1)
Instead of comparing each task, we can calculate the finish time for each task in a single pass and keep track of the minimum finish time.
⚙️
Algorithm
3 steps- 1Step 1: Initialize a variable to track the earliest finish time.
- 2Step 2: Loop through each task, compute finish[i] = s[i] + t[i].
- 3Step 3: Update the earliest finish time if the current finish time is smaller.
solution.py2 lines
1def earliestFinish(tasks):
2 return min(s + t for s, t in tasks)ℹ
Complexity note: This is O(n) because we only make a single pass through the tasks to compute finish times.
- 1Calculate finish time directly from start and duration.
- 2Use built-in functions for efficiency.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.