Advertisement

The Employee That Worked on the Longest Task - LeetCode 2432 Solution

The Employee That Worked on the Longest Task - Complete Solution Guide

The Employee That Worked on the Longest Task is LeetCode problem 2432, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] = [id i , leaveTime i ] where: id i is the id of the employee that worked on the i th task, and leaveTime i is the time at which the employee finished the i th task. All the values leaveTime i are unique . Note that the i th task starts the moment right after the (i - 1) th task ends, and the 0 th task starts at time 0 . Return the id of the employee that worked the task with the lo

Detailed Explanation

The problem asks to find the ID of the employee who worked on the longest task. The input is a 2D integer array `logs` where each inner array represents a task. `logs[i] = [id<sub>i</sub>, leaveTime<sub>i</sub>]` indicates employee `id<sub>i</sub>` finished task `i` at time `leaveTime<sub>i</sub>`. Tasks are sequential; task `i` starts immediately after task `i-1` ends (task 0 starts at time 0). The output is the ID of the employee who worked the longest; if there's a tie, return the smallest ID. The problem emphasizes that `leaveTime<sub>i</sub>` values are unique and strictly increasing.

Solution Approach

The solution iterates through the `logs` array. For each task, it calculates the task duration. It keeps track of the maximum duration encountered so far and the ID of the employee who worked that task. If a longer task is found, the maximum duration and employee ID are updated. If a task has the same duration as the current maximum, the employee ID is updated to the minimum of the current ID and the employee ID associated with the maximum duration. This ensures that the smallest employee ID is returned in case of ties.

Step-by-Step Algorithm

  1. Initialize `max_time` to the `leaveTime` of the first task and `employee_id` to the ID of the first task.
  2. Initialize `start_time` to 0.
  3. Iterate through the `logs` array:
  4. Calculate `current_time` as the difference between the current task's `leaveTime` and `start_time`.
  5. If `current_time` is greater than `max_time`, update `max_time` and `employee_id`.
  6. If `current_time` is equal to `max_time`, update `employee_id` to the minimum of `employee_id` and the current task's ID.
  7. Update `start_time` to the current task's `leaveTime`.
  8. Return `employee_id`.

Key Insights

  • The duration of a task is the difference between its `leaveTime` and the `leaveTime` of the previous task (or 0 for the first task).
  • We need to track both the maximum task duration and the ID of the employee who worked that task. The smallest ID should be prioritized in case of ties.
  • Iterating through the `logs` array once is sufficient to find the solution.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array.

Companies

Asked at: IBM, Morgan Stanley.