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

Master LeetCode problem 2432 (The Employee That Worked on the Longest Task), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2432. The Employee That Worked on the Longest Task

Easy

Problem Explanation

Explanation:

To solve this problem, we need to iterate over the given logs array and calculate the duration of each task for each employee. We can do this by keeping track of the current task end time for each employee. Then, we compare the duration of each task and find the one with the longest duration. If there is a tie, we return the employee with the smallest id.

  • We initialize a HashMap to store the end time of each task for each employee.
  • We iterate over the logs array and update the end time for each employee based on the current task.
  • We keep track of the employee with the longest task duration and the smallest id in case of a tie.
  • Finally, we return the employee with the longest task duration or the smallest id in case of a tie.

Time Complexity: O(n), where n is the number of tasks in the logs array. Space Complexity: O(n), to store the end time of tasks for each employee.

:

Solution Code

class Solution {
    public int longestWorkingEmployee(int n, int[][] logs) {
        HashMap<Integer, Integer> taskEndTime = new HashMap<>();
        int longestDuration = 0;
        int longestEmployee = 0;
        
        for (int[] log : logs) {
            int employeeId = log[0];
            int endTime = log[1];
            int startTime = taskEndTime.getOrDefault(employeeId, 0);
            int duration = endTime - startTime;
            
            if (duration > longestDuration || (duration == longestDuration && employeeId < longestEmployee)) {
                longestDuration = duration;
                longestEmployee = employeeId;
            }
            
            taskEndTime.put(employeeId, endTime);
        }
        
        return longestEmployee;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2432 (The Employee That Worked on the Longest Task)?

This page provides optimized solutions for LeetCode problem 2432 (The Employee That Worked on the Longest Task) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2432 (The Employee That Worked on the Longest Task)?

The time complexity for LeetCode 2432 (The Employee That Worked on the Longest Task) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2432 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2432 in Java, C++, or Python.

Back to LeetCode Solutions