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 thesmallest id among them.
Input: n =10, logs =[[0,3],[2,5],[0,9],[1,15]]Output: 1Explanation:
Task 0 started at 0 and ended at 3with3 units of times.Task 1 started at 3 and ended at 5with2 units of times.Task 2 started at 5 and ended at 9with4 units of times.Task 3 started at 9 and ended at 15with6 units of times.The task with the longest time is task 3 and the employee with id 1is the one that worked on it, so we return1.
Input: n =26, logs =[[1,1],[3,7],[2,12],[7,17]]Output: 3Explanation:
Task 0 started at 0 and ended at 1with1 unit of times.Task 1 started at 1 and ended at 7with6 units of times.Task 2 started at 7 and ended at 12with5 units of times.Task 3 started at 12 and ended at 17with5 units of times.The tasks with the longest time is task 1. The employee that worked on it is3, so we return3.
Input: n =2, logs =[[0,10],[1,20]]Output: 0Explanation:
Task 0 started at 0 and ended at 10with10 units of times.Task 1 started at 10 and ended at 20with10 units of times.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.
#include<vector>usingnamespace std;
inthardestWorker(int n, vector<vector<int>>& logs) {
int maxTime = logs[0][1], res = logs[0][0];
for (int i =1; i < logs.size(); ++i) {
int t = logs[i][1] - logs[i-1][1];
if (t > maxTime || (t == maxTime && logs[i][0] < res)) {
maxTime = t;
res = logs[i][0];
}
}
return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicinthardestWorker(int n, int[][] logs) {
int maxTime = logs[0][1], res = logs[0][0];
for (int i = 1; i < logs.length; ++i) {
int t = logs[i][1]- logs[i-1][1];
if (t > maxTime || (t == maxTime && logs[i][0]< res)) {
maxTime = t;
res = logs[i][0];
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
from typing import List
defhardestWorker(n: int, logs: List[List[int]]) -> int:
max_time = logs[0][1]
res = logs[0][0]
for i in range(1, len(logs)):
t = logs[i][1] - logs[i-1][1]
if t > max_time or (t == max_time and logs[i][0] < res):
max_time = t
res = logs[i][0]
return res
1
2
3
4
5
6
7
8
9
10
11
12
pubfnhardest_worker(_n: i32, logs: Vec<Vec<i32>>) -> i32 {
letmut max_time = logs[0][1];
letmut res = logs[0][0];
for i in1..logs.len() {
let t = logs[i][1] - logs[i-1][1];
if t > max_time || (t == max_time && logs[i][0] < res) {
max_time = t;
res = logs[i][0];
}
}
res
}