Menu Close

Program to find The Employee That Worked on the Longest Task

Here, we will see Program to find The Employee That Worked on the Longest Task with code and algorithm.

You are given 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
  • 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.

You have to 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.

A customer’s wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

Example:

1)
Input: n = 11, logs = [[1,4],[3,6],[1,10],[2,16]]
Output: 2
Explanation: 
Task 0 started at 1 and ended at 4 with 3 units of times.
Task 1 started at 4 and ended at 6 with 2 units of times.
Task 2 started at 6 and ended at 10 with 4 units of times.
Task 3 started at 10 and ended at 16 with 6 units of times.
The task with the longest time is task 3 and the employee with id 2 is the one that worked on it, so we return 2.

2)
Input: n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]
Output: 3
Explanation: 
Task 0 started at 0 and ended at 1 with 1 unit of times.
Task 1 started at 1 and ended at 7 with 6 units of times.
Task 2 started at 7 and ended at 12 with 5 units of times.
Task 3 started at 12 and ended at 17 with 5 units of times.
The tasks with the longest time is task 1. The employees that worked on it is 3, so we return 3.

3)
Input: n = 2, logs = [[0,10],[1,20]]
Output: 0
Explanation: 
Task 0 started at 0 and ended at 10 with 10 units of times.
Task 1 started at 10 and ended at 20 with 10 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.

Program to find The Employee That Worked on the Longest Task code in C++

Code 1:

#include <iostream>
#include <vector>

using namespace std;

int hardestWorker(int n, vector<vector<int>>& logs) {
    int max = logs[0][1], id = logs[0][0];
        
    for(int i = 1; i < logs.size(); i++) {
        if((logs[i][1] - logs[i-1][1]) > max) {
            max = logs[i][1] - logs[i-1][1];
            id = logs[i][0];
        } else if((logs[i][1] - logs[i-1][1]) == max) {
            if(logs[i][0] < id) {
                id = logs[i][0];
            }
        }
    }
    return id;
}
    
int main()
{
    int n = 10;
    vector<vector<int> > logs = {
        {1, 4}, 
        {3,6}, 
        {1,10}, 
        {2,16}
    };
    cout<<hardestWorker(n, logs);

    return 0;
}

Code 2:

#include <iostream>
#include <vector>

using namespace std;

int hardestWorker(int n, vector<vector<int>>& logs) {
    int start = 0;
    int res = -1;
    int time = -1;
    
    for (vector<int> & log : logs) {
        auto id = log[0];
        auto end = log[1];
        int duration = end-start;
        if (duration > time) {
            time = duration;
            res = id;
        } else if (duration == time && res > id) {
            res = id;
        }       
        start = end;
    }
    return res;
}
    
int main()
{
    int n = 10;
    vector<vector<int> > logs = {
        {1, 4}, 
        {3,6}, 
        {1,10}, 
        {2,16}
    };
    cout<<hardestWorker(n, logs);

    return 0;
}

Output:

2

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy

Leave a Reply

Your email address will not be published. Required fields are marked *