Leetcode 2432. The Employee That Worked on the Longest Task
A company assigns different tasks to its employees, where each task takes a certain amount of time to complete. The given tasks are tracked with their respective start times, and the system logs the task completion times in sequential order. Given a list of tasks, you need to determine which employee worked on the longest task. If there are multiple tasks with the same maximum duration, return the ID of the employee who completed the task first.
max_duration
) and the employee ID associated with it (employee_id
).max_duration
:
max_duration
and employee_id
.max_duration
, the first occurrence’s employee ID will be retained.employee_id
.public class Solution {
public int hardestWorker(int n, int[][] logs) {
int maxDuration = 0;
int employeeId = -1;
for (int i = 0; i < logs.length; i++) {
int startTime = logs[i][0];
int endTime = logs[i][1];
int currentEmployeeId = logs[i][2];
int duration = endTime - startTime;
if (duration > maxDuration || (duration == maxDuration && currentEmployeeId < employeeId)) {
maxDuration = duration;
employeeId = currentEmployeeId;
}
}
return employeeId;
}
}
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?