Skip to main content

Earliest Time to Finish One Task - Solution & Explanation

EasyArray5 min read
Practice this problem

Problem Statement

You are given a 2D integer array tasks where tasks[i] = [si, ti].

Each [si, ti] in tasks represents a task with start time si that takes ti units of time to finish.

Return the earliest time at which at least one task is finished.

 

Example 1:

Input: tasks = [[1,6],[2,3]]

Output: 5

Explanation:

The first task starts at time t = 1 and finishes at time 1 + 6 = 7. The second task finishes at time 2 + 3 = 5. You can finish one task at time 5.

Example 2:

Input: tasks = [[100,100],[100,100],[100,100]]

Output: 200

Explanation:

All three tasks finish at time 100 + 100 = 200.

 

Constraints:

  • 1 <= tasks.length <= 100
  • tasks[i] = [si, ti]
  • 1 <= si, ti <= 100

Approach Overview

Problem Overview: You receive two arrays representing tasks: a start time and the duration required to complete that task. The goal is to determine the earliest possible time at which any single task can finish. For each task, compute finish = start[i] + duration[i] and return the minimum finish time.

Approach 1: Sort by Finish Time (O(n log n) time, O(n) space)

A straightforward approach computes the finish time for every task and sorts them to find the smallest completion time. Create a list where each entry stores start[i] + duration[i]. After computing these values, sort the list and return the first element. Sorting guarantees the earliest completion appears at the front. This approach works but performs unnecessary work because you only need the minimum value. Sorting increases the time complexity to O(n log n) and requires O(n) extra space for the computed finish times.

Approach 2: Single Pass Minimum (O(n) time, O(1) space)

The optimal approach scans the arrays once while tracking the smallest completion time seen so far. For each index i, compute finish = start[i] + duration[i]. Compare it with the current minimum and update if it is smaller. After processing all tasks, the stored minimum represents the earliest possible completion time. This works because the problem reduces to finding the minimum value of a derived expression across an array. No additional data structures are required.

This pattern appears frequently in array scanning problems where the answer depends on a simple transformation of each element. Instead of storing intermediate results, compute the value and update a running minimum. The algorithm performs a constant amount of work per element, which keeps the time complexity at O(n) and space complexity at O(1). Similar techniques are used in problems involving minimum cost calculations or earliest event times.

Recommended for interviews: The single pass approach. Interviewers expect you to recognize that sorting is unnecessary when you only need the minimum value. Mentioning the sorting idea shows baseline reasoning, but implementing the linear scan demonstrates strong understanding of array traversal and optimal complexity analysis.

Solution

We iterate through the tasks array and, for each task, calculate its completion time s_i + t_i. The minimum of all task completion times is the earliest time to finish at least one task.

The time complexity is O(n), where n is the length of the tasks array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sort by Finish TimeO(n log n)O(n)Conceptual baseline when transforming tasks into finish times and sorting them
Single Pass MinimumO(n)O(1)Optimal solution when you only need the earliest finish time

Video Solution

LeetCode Weekly Contest 467 | Problems 3683 & 3684 | Full Step-by-Step Solution + Intuition • Navdeep R • 337 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Earliest Time to Finish One Task easy or hard?
Earliest Time to Finish One Task is considered an Easy problem. The key idea is recognizing that the result is simply the minimum value of start[i] + duration[i]. Once identified, the implementation becomes a straightforward single pass over the array.
Earliest Time to Finish One Task Python/Java solution
The implementation in Python or Java follows the same pattern: initialize a variable with a large value, loop through the arrays, compute start[i] + duration[i], and update the minimum. The logic remains identical across languages and executes in O(n) time with O(1) space.
How to solve Earliest Time to Finish One Task in O(n)?
Iterate through the arrays and compute the finish time for every task using finish = start[i] + duration[i]. Maintain a variable storing the smallest finish time encountered so far. Update it whenever a smaller value appears. After the loop ends, that minimum value is the earliest possible completion time.
What is the best approach for Earliest Time to Finish One Task?
The best approach is a single pass scan of the arrays. For each task compute finish time as start[i] + duration[i] and maintain the minimum value. This solution runs in O(n) time and O(1) space because it only tracks the smallest finish time while iterating once through the array.
Is Earliest Time to Finish One Task asked at Google/Amazon/Meta?
Problems involving earliest completion times and array scans frequently appear in interviews at companies like Amazon and Google. While the exact question may vary, the underlying concept of computing a derived value and tracking the minimum during a single pass is a common interview pattern.
What data structure is used in Earliest Time to Finish One Task?
The problem primarily uses arrays. The algorithm performs a linear traversal of the start and duration arrays while maintaining a running minimum. No additional structures such as heaps or hash maps are necessary for the optimal solution.
What is the time complexity of Earliest Time to Finish One Task?
The optimal solution runs in O(n) time because each task is processed exactly once. Only a constant amount of work is performed per iteration: computing start[i] + duration[i] and updating the minimum value. Space complexity remains O(1) since no additional data structures are required.

Ready to solve this problem?

Practice Earliest Time to Finish One Task with our built-in code editor and test cases.

Practice on FleetCode