The Employee That Worked on the Longest Task - Problem
There are n employees, each with a unique id from 0 to n - 1.
You are given a 2D integer array logs where logs[i] = [idi, leaveTimei] where:
idiis the id of the employee that worked on theith task, andleaveTimeiis the time at which the employee finished theith task. All the valuesleaveTimeiare unique.
Note that the ith task starts the moment right after the (i - 1)th task ends, and the 0th task starts at time 0.
Return the id of the employee that worked the task with the longest time. If there is a tie between two or more employees, return the smallest id among them.
Input & Output
Example 1 — Basic Case
$
Input:
logs = [[0,3],[2,5],[0,9],[1,15]]
›
Output:
1
💡 Note:
Task durations: [3-0=3, 5-3=2, 9-5=4, 15-9=6]. Employee 1 worked the longest task (6 time units).
Example 2 — Tie Breaking
$
Input:
logs = [[1,1],[2,3],[3,4]]
›
Output:
1
💡 Note:
Task durations: [1-0=1, 3-1=2, 4-3=1]. Employee 2 has the longest task (2 time units).
Example 3 — Same Duration Different IDs
$
Input:
logs = [[2,4],[1,7],[3,8]]
›
Output:
1
💡 Note:
Task durations: [4-0=4, 7-4=3, 8-7=1]. Employee 2 worked the longest task (4 time units).
Constraints
- 1 ≤ logs.length ≤ 500
- logs[i].length == 2
- 0 ≤ idi ≤ 499
- 1 ≤ leaveTimei ≤ 500
- idi != idi+1
- leaveTimei are sorted in a strictly increasing order
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Parse logs with [employeeId, leaveTime] pairs
2
Duration Calculation
Calculate task duration as current_leave_time - previous_leave_time
3
Find Maximum
Return employee ID with longest task duration
Key Takeaway
🎯 Key Insight: Task duration = current leave time - previous leave time, find employee with maximum duration
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code