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
Employee Task Duration VisualizationSequential Task Execution:TimelineStart: 0Employee 0Duration: 33Employee 2Duration: 25Employee 0Duration: 49Employee 1 โญDuration: 6 (LONGEST!)15Algorithm Steps:Step 1: InitializemaxDuration = 0bestEmployee = 0prevTime = 0Step 2: Process LogsFor each log entry:duration = leaveTime - prevTimeUpdate max if neededStep 3: Handle TiesIf same duration,choose smalleremployee IDStep 4: ReturnEmployee ID withlongest duration๐ŸŽฏ Key InsightSince tasks run sequentially without gaps,each task duration = current_leave_time - previous_leave_timeThis allows us to solve in O(n) time and O(1) space!
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.
Asked in
Amazon 15 Google 8 Microsoft 6 Meta 4
26.8K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen