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:
idi
is the id of the employee that worked on the ith
task, andleaveTimei
is the time at which the employee finished the ith
task. All the values leaveTimei
are 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.
Example 1:
Input: n = 10, logs = [[0,3],[2,5],[0,9],[1,15]] Output: 1 Explanation: Task 0 started at 0 and ended at 3 with 3 units of times. Task 1 started at 3 and ended at 5 with 2 units of times. Task 2 started at 5 and ended at 9 with 4 units of times. Task 3 started at 9 and ended at 15 with 6 units of times. The task with the longest time is task 3 and the employee with id 1 is the one that worked on it, so we return 1.
Example 2:
Input: n = 26, logs = [[1,1],[3,7],[2,12],[7,17]] Output: 3 Explanation: Task 0 started at 0 and ended at 1 with 1 unit of times. Task 1 started at 1 and ended at 7 with 6 units of times. Task 2 started at 7 and ended at 12 with 5 units of times. Task 3 started at 12 and ended at 17 with 5 units of times. The tasks with the longest time is task 1. The employee that worked on it is 3, so we return 3.
Example 3:
Input: n = 2, logs = [[0,10],[1,20]] Output: 0 Explanation: Task 0 started at 0 and ended at 10 with 10 units of times. Task 1 started at 10 and ended at 20 with 10 units of times. The tasks with the longest time are tasks 0 and 1. The employees that worked on them are 0 and 1, so we return the smallest id 0.
Constraints:
2 <= n <= 500
1 <= logs.length <= 500
logs[i].length == 2
0 <= idi <= n - 1
1 <= leaveTimei <= 500
idi != idi+1
leaveTimei
are sorted in a strictly increasing order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the employee who worked on the longest task using brute force, we check each employee's task durations one by one. We compare these durations to find the largest one. This is like checking every possible answer to find the best one.
Here's how the algorithm would work step-by-step:
def find_employee_with_longest_task(tasks):
employee_times = {}
for task in tasks:
employee_id = task[0]
start_time = task[1]
end_time = task[2]
# Calculate task duration for the current employee
task_duration = end_time - start_time
# If employee not yet in dictionary, add them
if employee_id not in employee_times:
employee_times[employee_id] = 0
employee_times[employee_id] += task_duration
employee_with_longest_task = None
longest_time = 0
# Determine employee with the longest time working on tasks.
for employee_id, total_time in employee_times.items():
if total_time > longest_time:
#Store the employee ID and their total time working
employee_with_longest_task = employee_id
longest_time = total_time
return employee_with_longest_task
To find the employee who worked on the longest task, we'll go through each employee's tasks and calculate the duration of each task. Then, we'll keep track of the employee with the longest single task duration found so far, updating it whenever we find a longer one.
Here's how the algorithm would work step-by-step:
def find_employee_with_longest_task(tasks):
longest_task_duration = 0
employee_with_longest_task = None
for task in tasks:
employee_id = task['employee_id']
task_duration = task['duration']
# Update if current task is longer.
if task_duration > longest_task_duration:
longest_task_duration = task_duration
employee_with_longest_task = employee_id
# After iterating, return the employee ID and max duration.
return employee_with_longest_task, longest_task_duration
def find_employee_with_longest_task_alternative(tasks):
max_duration = 0
employee_id_with_max_duration = None
for task in tasks:
current_employee_id = task['employee_id']
current_duration = task['duration']
# Core logic: Compare the current duration.
if current_duration > max_duration:
# Update the maximum duration.
max_duration = current_duration
# Save the employee ID with max duration.
employee_id_with_max_duration = current_employee_id
# Return the final result.
return employee_id_with_max_duration, max_duration
Case | How to Handle |
---|---|
Empty logs array | Return -1 since there are no tasks to consider. |
Null logs array | Return -1 or throw an IllegalArgumentException to signal invalid input. |
n is 0 or negative, but logs is not empty | Handle this by either ignoring n and processing the logs, or throw an IllegalArgumentException if n is required for processing. |
Logs contains only one task | Return the employee ID of that single task since it is the longest. |
Logs contains tasks with the same duration but different employee IDs | The solution should iterate through the logs and keep track of the longest duration and the smallest ID among those with the longest duration. |
Large leaveTime values potentially leading to integer overflow when calculating duration if not handled carefully | Use long data type for duration calculation to prevent potential overflow. |
Logs are not sorted by leaveTime | The solution must correctly calculate duration using the previous leaveTime or assume a starting time of 0 and process correctly. |
Duplicate employee IDs with differing leave times | The algorithm correctly updates the last leave time of the specified employee based on the given employee ID if necessary. |