|
| 1 | +--- |
| 2 | +description: 'Author: @heder | https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/' |
| 3 | +tags: [Array] |
| 4 | +--- |
| 5 | + |
| 6 | +# 2432 - The Employee That Worked on the Longest Task (Easy) |
| 7 | + |
| 8 | +## Problem Link |
| 9 | + |
| 10 | +https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/ |
| 11 | + |
| 12 | +## Problem Statement |
| 13 | + |
| 14 | +There are `n` employees, each with a unique id from `0` to `n - 1`. |
| 15 | + |
| 16 | +You are given a 2D integer array `logs` where `logs[i] = [idi, leaveTimei]` where: |
| 17 | + |
| 18 | +- `idi` is the id of the employee that worked on the `ith` task, and |
| 19 | +- `leaveTimei` is the time at which the employee finished the `ith` task. All the values `leaveTimei` are **unique**. |
| 20 | + |
| 21 | +Note that the `ith` task starts the moment right after the `(i - 1)th` task ends, and the `0th` task starts at time `0`. |
| 22 | + |
| 23 | +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*. |
| 24 | + |
| 25 | +**Example 1:** |
| 26 | + |
| 27 | +``` |
| 28 | +Input: n = 10, logs = [[0,3],[2,5],[0,9],[1,15]] |
| 29 | +Output: 1 |
| 30 | +Explanation: |
| 31 | +Task 0 started at 0 and ended at 3 with 3 units of times. |
| 32 | +Task 1 started at 3 and ended at 5 with 2 units of times. |
| 33 | +Task 2 started at 5 and ended at 9 with 4 units of times. |
| 34 | +Task 3 started at 9 and ended at 15 with 6 units of times. |
| 35 | +The task with the longest time is task 3 and the employee with id 1 is the one that worked on it, so we return 1. |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 2:** |
| 39 | + |
| 40 | +``` |
| 41 | +Input: n = 26, logs = [[1,1],[3,7],[2,12],[7,17]] |
| 42 | +Output: 3 |
| 43 | +Explanation: |
| 44 | +Task 0 started at 0 and ended at 1 with 1 unit of times. |
| 45 | +Task 1 started at 1 and ended at 7 with 6 units of times. |
| 46 | +Task 2 started at 7 and ended at 12 with 5 units of times. |
| 47 | +Task 3 started at 12 and ended at 17 with 5 units of times. |
| 48 | +The tasks with the longest time is task 1. The employee that worked on it is 3, so we return 3. |
| 49 | +``` |
| 50 | + |
| 51 | +**Example 3:** |
| 52 | + |
| 53 | +``` |
| 54 | +Input: n = 2, logs = [[0,10],[1,20]] |
| 55 | +Output: 0 |
| 56 | +Explanation: |
| 57 | +Task 0 started at 0 and ended at 10 with 10 units of times. |
| 58 | +Task 1 started at 10 and ended at 20 with 10 units of times. |
| 59 | +The tasks with the longest time are tasks 0 and 1. The employees that worked on them are 0 and 1, so we return the smallest id 0. |
| 60 | +``` |
| 61 | + |
| 62 | +**Constraints:** |
| 63 | + |
| 64 | +- `2 <= n <= 500` |
| 65 | +- `1 <= logs.length <= 500` |
| 66 | +- `logs[i].length == 2` |
| 67 | +- `0 <= idi <= n - 1` |
| 68 | +- `1 <= leaveTimei <= 500` |
| 69 | +- `idi != idi+1` |
| 70 | +- `leaveTimei` are sorted in a strictly increasing order. |
| 71 | + |
| 72 | +## Approach 1: Single Pass |
| 73 | + |
| 74 | +This could code be a little bit more compact, but I like unpacking the input and giving it things meaningful names. |
| 75 | + |
| 76 | +- Time complexity: $$O(size(logs))$$ we need to look at all the entries in logs. |
| 77 | +- Space complexity: $$O(1)$$ only a few integers as state. |
| 78 | + |
| 79 | + |
| 80 | +<Tabs> |
| 81 | +<TabItem value="cpp" label="C++"> |
| 82 | +<SolutionAuthor name="@heder"/> |
| 83 | + |
| 84 | +```cpp |
| 85 | +static int hardestWorker(int n, const vector<vector<int>>& logs) { |
| 86 | + int start_time = 0; |
| 87 | + int worker = n; |
| 88 | + int longest_task = 0; |
| 89 | + for (const vector<int>& log : logs) { |
| 90 | + const int id = log[0]; |
| 91 | + const int end_time = log[1]; |
| 92 | + const int time = end_time - start_time; |
| 93 | + |
| 94 | + if (time > longest_task || time == longest_task && id < worker) { |
| 95 | + worker = id; |
| 96 | + longest_task = time; |
| 97 | + } |
| 98 | + |
| 99 | + start_time = end_time; |
| 100 | + } |
| 101 | + return worker; |
| 102 | +} |
| 103 | +``` |
| 104 | +
|
| 105 | +</TabItem> |
| 106 | +</Tabs> |
0 commit comments