Approaches
💡
Intuition
Time UnknownSpace Unknown
In the optimal solution, we keep track of the total time each employee worked using a single pass through the logs. This approach is efficient and avoids unnecessary computations.
⚙️
Algorithm
5 steps- 1Step 1: Initialize an array to store the total time for each employee.
- 2Step 2: Initialize a variable to keep track of the start time of the current task.
- 3Step 3: Iterate through the logs, calculating the duration of each task and updating the corresponding employee's total time.
- 4Step 4: After processing all logs, find the employee with the maximum time worked.
- 5Step 5: If there's a tie, return the employee with the smallest ID.
solution.py12 lines
1# Full working Python code
2n = 10
3logs = [[0,3],[2,5],[0,9],[1,15]]
4times = [0] * n
5start_time = 0
6for id, leaveTime in logs:
7 duration = leaveTime - start_time
8 times[id] += duration
9 start_time = leaveTime
10max_time = max(times)
11result = min(i for i, t in enumerate(times) if t == max_time)
12print(result)Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.