You are given n
tasks that are assigned to n
employees. Each task is represented by a tuple (employee_id, timestamp)
, where employee_id
is the employee’s ID that completes the task, and timestamp
is the time the task is completed. The list of tasks is provided in non-decreasing order of timestamps.
Your goal is to find out which employee has worked the longest on a single task. If there is a tie, return the employee with the smallest ID.
Write a function:
def hardestWorker(n: int, logs: List[List[int]]) -> int:
n
: integer, number of employeeslogs
: list of lists, each inner list contains 2 integers representing employee_id
and timestamp
Return the employee_id
of the employee who worked the longest on a single task.
logs
list?
n
and the size of logs
?
logs
list.The algorithm primarily involves iterating through the list of logs once, so the time complexity is O(m), where m is the length of the logs
list.
Here is an implementation of the solution:
from typing import List
def hardestWorker(n: int, logs: List[List[int]]) -> int:
max_time = 0
hardest_worker = None # Initially, no hardest worker identified
# Starting from first task's completion time
prev_time = 0 # Beginning with 0 timestamp
for log in logs:
emp_id, curr_time = log
time_spent = curr_time - prev_time
if time_spent > max_time:
max_time = time_spent
hardest_worker = emp_id
elif time_spent == max_time:
if emp_id < hardest_worker:
hardest_worker = emp_id
prev_time = curr_time # Update previous time to current time
return hardest_worker
In this implementation, we:
max_time
as 0 to track the longest task time and hardest_worker
as None
.prev_time
to keep track of the end time of the previous task.logs
to calculate the duration of each task and update max_time
and hardest_worker
accordingly.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?