[LeetCode] The Employee That Worked on the Longest Task

2432. The Employee That Worked on the Longest Task

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int hardestWorker(int n, vector<vector<int>>& logs) {
int now = 0, ma = 0;
unordered_map<int, int> mp;
for(int i = 0; i < logs.size(); i++) {
int id = logs[i][0], end = logs[i][1];
int time = end - now;
mp[id] = max(mp[id],time);
if(mp[ma] < mp[id]) ma = id;
else if(mp[ma] == mp[id]) ma = min(ma,id);
now = end;
}
return ma;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/09/PS/LeetCode/the-employee-that-worked-on-the-longest-task/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.