Date: 2026-06-06
Time: 19:27
the-employee-that-worked-on-the-longest-task/solution.pyThis file solves LeetCode 2432: The Employee That Worked on the Longest Task. It owns the single responsibility of determining which employee worked the longest uninterrupted task, given a chronological log of task completions.
workerwithlongest_task(n, logs) — The sole public function.
n (number of employees, unused in the logic but part of the LeetCode signature), logs (a list of [employeeid, leavetime] pairs sorted ascending by leave_time).employeeid of whoever worked the single longest task. On a tie, the smallest employeeid wins.Single-pass greedy scan. The function iterates through logs once, maintaining the best candidate seen so far. This is the canonical pattern for "find the max/min with a tiebreaker" problems — O(n) time, O(1) space.
Implicit first-task handling. The first entry's duration is logs[0][1] - 0, which equals logs[0][1]. The code seeds best_duration with logs[0][1] directly, avoiding an explicit subtraction from zero. The loop then starts at index 1, computing subsequent durations as deltas between consecutive leave times.
Imports: None. Pure function with no external dependencies.
Imported by: The test_solution.py in the same directory. The "Imported By" list in the prompt is misleading — those are unrelated test files across the repo, likely an artifact of a shared test runner or import-scanning tool, not actual consumers of this function.
1. Seed bestid and bestduration from the first log entry (task started at time 0, ended at logs[0][1]).
2. Track prev_time as the end of the last task.
3. For each subsequent entry i:
duration = logs[i][1] - prev_time.prev_time.4. Return best_id.
logs is non-empty and sorted by leave time. The code indexes logs[0] unconditionally — an empty list would crash.duration == bestduration and logs[i][0] < bestid ensures that if two tasks have equal length, the employee with the lower id is retained. This matches the problem's specification exactly.n is accepted but unused. The employee count doesn't affect the algorithm; it exists only to match the LeetCode function signature.None. The function trusts its inputs entirely — no validation of list bounds, types, or sort order. This is standard for LeetCode solutions operating within guaranteed constraints.