EasyNo. 2432

The Employee That Worked on the Longest Task — LeetCode 2432 Python Solution

Topics
Array
Reading time
2 min

The problem

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, and leaveTimei is the time at which the employee finished the ith task.

Example

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.

Python solution

PythonSolution
class Solution:
    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:
        last = mx = ans = 0
        for uid, t in logs:
            t -= last
            if mx < t or (mx == t and ans > uid):
                ans, mx = uid, t
            last += t
        return ans

Complexity

Time
O(n)where n is the length of the array logs
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 2432. The Employee That Worked on the Longest Task?

LeetCode 2432. The Employee That Worked on the Longest Task is rated Easy on LeetCode.

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

The Python solution on this page runs in O(n), where n is the length of the array logs.

What is the space complexity of LeetCode 2432. The Employee That Worked on the Longest Task?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 2432. The Employee That Worked on the Longest Task cover?

LeetCode 2432. The Employee That Worked on the Longest Task is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.