Skip to main content
Back to problems
#2432
Easy Algorithms

The employee that worked on the longest task

Array
51.4% acceptance
Feb 25, 2026
294
72
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. All the v alues leaveTimei are unique. * Note that the ith task starts the moment right after the (i - 1)th task ends, and the 0th task starts at time 0. * Return the id of the employee that worked the task with the longest time. If there is a tie between two or more employees, return the smallest id among them. *

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn hardest_worker(_n: i32, logs: Vec<Vec<i32>>) -> i32 {
    let mut best_id = logs[0][0];
    let mut best_time = logs[0][1];
    for i in 1..logs.len() {
      let time = logs[i][1] - logs[i - 1][1];
      if time > best_time || (time == best_time && logs[i][0] < best_id) {
        best_time = time;
        best_id = logs[i][0];
      }
    }
    best_id
  }
}