The Employee That Worked on the Longest Task - Problem
You're managing a team of n employees in a busy workplace, each with a unique ID from 0 to n - 1. Throughout the day, tasks are completed in sequence, and you need to track which employee worked the longest on a single task.
You're given a 2D array logs where logs[i] = [employeeId, leaveTime] represents:
- employeeId: The ID of the employee who completed the
i-th task - leaveTime: The exact time when this employee finished the task
Important timing rules:
- Task 0 starts at time
0 - Each subsequent task starts immediately when the previous task ends
- Task duration =
leaveTime - startTime
Goal: Find the employee ID who worked on the task with the longest duration. In case of a tie, return the employee with the smallest ID.
Example: If logs = [[0,3],[2,5],[0,9],[1,15]], the task durations are [3,2,4,6] and employee 1 worked the longest (6 time units).
Input & Output
example_1.py โ Basic Case
$
Input:
n = 10, logs = [[0,3],[2,5],[0,9],[1,15]]
โบ
Output:
1
๐ก Note:
Task durations: [3,2,4,6]. Employee 1 worked on the longest task (6 time units) from time 9 to 15.
example_2.py โ Tie Breaking
$
Input:
n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]
โบ
Output:
3
๐ก Note:
Task durations: [1,6,5,5]. Both employees 3 and 7 worked for 5-6 time units, but employee 3 has smaller ID.
example_3.py โ Single Task
$
Input:
n = 2, logs = [[0,10]]
โบ
Output:
0
๐ก Note:
Only one task with duration 10. Employee 0 is the answer.
Constraints
- 2 โค n โค 500
- 1 โค logs.length โค 500
- logs[i].length == 2
- 0 โค idi โค n - 1
- 1 โค leaveTimei โค 500
- idi != idi+1
- All leaveTimei are unique and in strictly increasing order
Visualization
Tap to expand
Understanding the Visualization
1
Sequential Tasks
Tasks run one after another, each employee's duration = finish_time - start_time
2
Track Maximum
Keep track of longest duration seen so far and which employee achieved it
3
Handle Ties
If two employees have same duration, choose the one with smaller ID
Key Takeaway
๐ฏ Key Insight: Sequential task execution means we can calculate durations on-the-fly in a single pass, achieving optimal O(n) time and O(1) space complexity.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code