#3683

Earliest Time to Finish One Task

Easy
ArrayArrayMin/Max Search
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal 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
  1. 1Step 1: Initialize a variable to track the earliest finish time.
  2. 2Step 2: Loop through each task, compute finish[i] = s[i] + t[i].
  3. 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.