Skip to content

Commit 1db4968

Browse files
authored
Merge pull request #4170 from AmruthaPariprolu/add/2432
2432 added
2 parents b445009 + 3d666c5 commit 1db4968

File tree

1 file changed

+300
-0
lines changed

1 file changed

+300
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
---
2+
id: The-Employee-That-Worked-on-the-Longest-Task
3+
title: The Employee That Worked on the Longest Task
4+
sidebar_label: 2432-The Employee That Worked on the Longest Task
5+
tags: [dsa, leetcode]
6+
description: Problem solution of The Employee That Worked on the Longest Task
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
There are n employees, each with a unique id from 0 to n - 1.
14+
15+
You are given a 2D integer array logs where logs[i] = [idi, leaveTimei] where:
16+
17+
idi is the id of the employee that worked on the ith task, and
18+
leaveTimei is the time at which the employee finished the ith task. All the values leaveTimei are unique.
19+
Note that the ith task starts the moment right after the (i - 1)th task ends, and the 0th task starts at time 0.
20+
21+
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.
22+
23+
24+
25+
26+
### Examples
27+
28+
#### Example 1
29+
```
30+
Input: n = 10, logs = [[0,3],[2,5],[0,9],[1,15]]
31+
Output: 1
32+
Explanation:
33+
Task 0 started at 0 and ended at 3 with 3 units of times.
34+
Task 1 started at 3 and ended at 5 with 2 units of times.
35+
Task 2 started at 5 and ended at 9 with 4 units of times.
36+
Task 3 started at 9 and ended at 15 with 6 units of times.
37+
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.
38+
```
39+
40+
### Example 2
41+
```
42+
Input: n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]
43+
Output: 3
44+
Explanation:
45+
Task 0 started at 0 and ended at 1 with 1 unit of times.
46+
Task 1 started at 1 and ended at 7 with 6 units of times.
47+
Task 2 started at 7 and ended at 12 with 5 units of times.
48+
Task 3 started at 12 and ended at 17 with 5 units of times.
49+
The tasks with the longest time is task 1. The employee that worked on it is 3, so we return 3.
50+
```
51+
52+
### Example 3
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+
## Solution of Given Problem
73+
74+
### Intuition and Approach
75+
76+
The problem can be solved using a brute force approach or an optimized Technique.
77+
78+
<Tabs>
79+
<tabItem value="Brute Force" label="Brute Force">
80+
81+
### Approach 1:Brute Force (Naive)
82+
83+
84+
Brute Force Approach:
85+
- Initialize variables to keep track of the maximum time and the corresponding employee id.
86+
- Iterate through the logs array.
87+
- For each task, calculate the time taken.
88+
- Compare the calculated time with the maximum time and update the variables accordingly.
89+
- Return the employee id with the longest task time.
90+
#### Codes in Different Languages
91+
92+
<Tabs>
93+
<TabItem value="C++" label="C++" default>
94+
<SolutionAuthor name="@AmruthaPariprolu"/>
95+
96+
```cpp
97+
#include <vector>
98+
#include <algorithm>
99+
#include <climits>
100+
using namespace std;
101+
102+
int hardestWorker(int n, vector<vector<int>>& logs) {
103+
int maxTime = INT_MIN;
104+
int employeeId = INT_MAX;
105+
106+
for (int i = 0; i < logs.size(); ++i) {
107+
int time = (i == 0) ? logs[i][1] : logs[i][1] - logs[i-1][1];
108+
if (time > maxTime || (time == maxTime && logs[i][0] < employeeId)) {
109+
maxTime = time;
110+
employeeId = logs[i][0];
111+
}
112+
}
113+
return employeeId;
114+
}
115+
116+
```
117+
</TabItem>
118+
<TabItem value="Java" label="Java">
119+
<SolutionAuthor name="@AmruthaPariprolu"/>
120+
121+
```java
122+
import java.util.List;
123+
124+
public class Solution {
125+
public int hardestWorker(int n, List<int[]> logs) {
126+
int maxTime = Integer.MIN_VALUE;
127+
int employeeId = Integer.MAX_VALUE;
128+
129+
for (int i = 0; i < logs.size(); ++i) {
130+
int time = (i == 0) ? logs.get(i)[1] : logs.get(i)[1] - logs.get(i - 1)[1];
131+
if (time > maxTime || (time == maxTime && logs.get(i)[0] < employeeId)) {
132+
maxTime = time;
133+
employeeId = logs.get(i)[0];
134+
}
135+
}
136+
return employeeId;
137+
}
138+
}
139+
140+
141+
142+
143+
144+
```
145+
146+
147+
</TabItem>
148+
<TabItem value="Python" label="Python">
149+
<SolutionAuthor name="@AmruthaPariprolu"/>
150+
151+
```python
152+
def hardestWorker(n, logs):
153+
max_time = float('-inf')
154+
employee_id = float('inf')
155+
156+
for i in range(len(logs)):
157+
time = logs[i][1] if i == 0 else logs[i][1] - logs[i - 1][1]
158+
if time > max_time or (time == max_time and logs[i][0] < employee_id):
159+
max_time = time
160+
employee_id = logs[i][0]
161+
162+
return employee_id
163+
164+
```
165+
166+
</TabItem>
167+
</Tabs>
168+
169+
170+
### Complexity Analysis
171+
172+
- Time Complexity: $O(n)$
173+
- We iterate through the logs array once, performing constant time operations for each log.
174+
- Space Complexity: $O(1)$
175+
- We use a constant amount of extra space for variables.
176+
177+
</tabItem>
178+
<tabItem value="Optimized approach" label="Optimized approach">
179+
180+
### Approach 2: Optimized approach
181+
182+
Optimized Approach:
183+
- Initialize variables to keep track of the maximum time and the corresponding employee id.
184+
- Iterate through the logs array.
185+
- For each task, calculate the time taken directly using the leave times of consecutive tasks.
186+
- Compare the calculated time with the maximum time and update the variables accordingly.
187+
- Return the employee id with the longest task time.
188+
189+
#### Code in Different Languages
190+
191+
<Tabs>
192+
<TabItem value="C++" label="C++" default>
193+
<SolutionAuthor name="@AmruthaPariprolu"/>
194+
195+
```cpp
196+
#include <vector>
197+
#include <algorithm>
198+
#include <climits>
199+
using namespace std;
200+
201+
int hardestWorker(int n, vector<vector<int>>& logs) {
202+
int maxTime = logs[0][1];
203+
int employeeId = logs[0][0];
204+
205+
for (int i = 1; i < logs.size(); ++i) {
206+
int time = logs[i][1] - logs[i-1][1];
207+
if (time > maxTime || (time == maxTime && logs[i][0] < employeeId)) {
208+
maxTime = time;
209+
employeeId = logs[i][0];
210+
}
211+
}
212+
return employeeId;
213+
}
214+
215+
216+
217+
```
218+
</TabItem>
219+
<TabItem value="Java" label="Java">
220+
<SolutionAuthor name="@AmruthaPariprolu"/>
221+
222+
```java
223+
import java.util.List;
224+
225+
public class Solution {
226+
public int hardestWorker(int n, List<int[]> logs) {
227+
int maxTime = logs.get(0)[1];
228+
int employeeId = logs.get(0)[0];
229+
230+
for (int i = 1; i < logs.size(); ++i) {
231+
int time = logs.get(i)[1] - logs.get(i - 1)[1];
232+
if (time > maxTime || (time == maxTime && logs.get(i)[0] < employeeId)) {
233+
maxTime = time;
234+
employeeId = logs.get(i)[0];
235+
}
236+
}
237+
return employeeId;
238+
}
239+
}
240+
241+
242+
243+
```
244+
245+
246+
</TabItem>
247+
<TabItem value="Python" label="Python">
248+
<SolutionAuthor name="@AmruthaPariprolu"/>
249+
250+
```python
251+
def hardestWorker(n, logs):
252+
max_time = logs[0][1]
253+
employee_id = logs[0][0]
254+
255+
for i in range(1, len(logs)):
256+
time = logs[i][1] - logs[i - 1][1]
257+
if time > max_time or (time == max_time and logs[i][0] < employee_id):
258+
max_time = time
259+
employee_id = logs[i][0]
260+
261+
return employee_id
262+
263+
264+
```
265+
266+
</TabItem>
267+
</Tabs>
268+
269+
#### Complexity Analysis
270+
271+
- Time Complexity: $O(n)$
272+
- We iterate through the logs array once, calculating task durations directly.
273+
- Space Complexity: $O(1)$
274+
- We use a constant amount of extra space for variables.
275+
- This approach is efficient and straightforward.
276+
277+
</tabItem>
278+
</Tabs>
279+
280+
281+
## Video Explanation of Given Problem
282+
283+
<LiteYouTubeEmbed
284+
id="YIpCPgwWKLY"
285+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
286+
title="Problem Explanation | Solution | Approach"
287+
poster="maxresdefault"
288+
webp
289+
/>
290+
291+
---
292+
293+
<h2>Authors:</h2>
294+
295+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
296+
{['AmruthaPariprolu'].map(username => (
297+
<Author key={username} username={username} />
298+
))}
299+
</div>
300+

0 commit comments

Comments
 (0)