Earliest Time to Finish One Task Problem
#3683
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3683.
You are given a 2D integer array tasks where tasks[i] = [s_i, t_i].
Each [s_i, t_i] in tasks represents a task with start time s_i that takes t_i 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 <= 100tasks[i] = [s_i, t_i]1 <= s_i, t_i <= 100
Solution
Approach: Single Pass
Task i finishes at time s_i + t_i, and the tasks are independent. The earliest time at which some task is done is the minimum of s_i + t_i over all tasks.
Complexity: O(n) time and O(1) extra space, where n is the number of tasks.
class Solution:
def earliestTime(self, tasks: List[List[int]]) -> int:
return min(s + t for s, t in tasks)