Skip to content

3683. Earliest Time to Finish One Task

Description

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

Solutions

Solution 1: Single Pass

We iterate through the \(\textit{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 \(\textit{tasks}\) array. The space complexity is \(O(1)\).

1
2
3
class Solution:
    def earliestTime(self, tasks: List[List[int]]) -> int:
        return min(s + t for s, t in tasks)
1
2
3
4
5
6
7
8
9
class Solution {
    public int earliestTime(int[][] tasks) {
        int ans = 200;
        for (var task : tasks) {
            ans = Math.min(ans, task[0] + task[1]);
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
public:
    int earliestTime(vector<vector<int>>& tasks) {
        int ans = 200;
        for (const auto& task : tasks) {
            ans = min(ans, task[0] + task[1]);
        }
        return ans;
    }
};
1
2
3
4
5
6
7
func earliestTime(tasks [][]int) int {
    ans := 200
    for _, task := range tasks {
        ans = min(ans, task[0]+task[1])
    }
    return ans
}
1
2
3
function earliestTime(tasks: number[][]): number {
    return Math.min(...tasks.map(task => task[0] + task[1]));
}

Comments