-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2432-the-employee-that-worked-on-the-longest-task.js
More file actions
40 lines (37 loc) · 1.23 KB
/
2432-the-employee-that-worked-on-the-longest-task.js
File metadata and controls
40 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 2432. The Employee That Worked on the Longest Task
* https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/
* Difficulty: Easy
*
* 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 values
* 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.
*/
/**
* @param {number} n
* @param {number[][]} logs
* @return {number}
*/
var hardestWorker = function(n, logs) {
let maxDuration = 0;
let result = n;
let startTime = 0;
for (const [employeeId, endTime] of logs) {
const duration = endTime - startTime;
if (duration > maxDuration || (duration === maxDuration && employeeId < result)) {
maxDuration = duration;
result = employeeId;
}
startTime = endTime;
}
return result;
};