LC.P2432[处理用时最长的那个任务的员工]

方法一:直接遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int hardestWorker(int n, int[][] logs) {
int ans = 0, max = 0, last = 0;
for (int[] log : logs) {
int id = log[0], time = log[1];
time -= last;
if (max < time || (max == time && ans > id)) {
max = time;
ans = id;
}
last += time;
}
return ans;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$