File: the-employee-that-worked-on-the-longest-task/solution.py

Date: 2026-06-06

Time: 19:27

the-employee-that-worked-on-the-longest-task/solution.py

Purpose

This 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.

Key Components

workerwithlongest_task(n, logs) — The sole public function.

Patterns

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.

Dependencies

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.

Flow

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:

4. Return best_id.

Invariants

Error Handling

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.